Friday, 9 March 2012

Android - Application development issues and solution

Here are some quick tips that will come in handy when developing an android application.


S.No
Issue
Solution
1.
How to remove focus for an EditText control? (When a new screen is launched with EditText, keypad appears automatically hiding the screen layout).
Set android:focusableInTouchMode="true" in the layout where the EditText is coded to remove the automatic setting of focus. 
2.
How to add an image in a Button control?
Set android:drawableright=”drawable/image1”  in the Button layout to add the image to the right and  
android:drawableleft =”drawable/image1”  to add the image to the left.

3.
How to remove the background from a button and make it transparent?

Set android:background=”@android: color/transparent” in the Button layout

4.
How to allow signed numbers in EditText?
Set android:inputType=”numberDecimal/numberSigned” in the EditText layout.

5.
How to set gradient colour for a button?
Create an xml with shape object and give the gradient colours

<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<gradient
    android:endColor="#9BBB59”
    android:startColor="#4F6228”
    android:angle="90" />       
</shape>

Include this xml in the drawable and give as background for the button.

6.
How to set a common background for 2 or more controls like a Button , Textview  and EditText?

Use a <Framelayout> and set the background colour or image in the layout. Include the controls inside the layout.

7.
How to manage multiple screen layouts in a single activity?
Use <ViewFlipper> and flip the screen layouts in the code.
8.
How to declare global variables to be used by all activities in a package?
Create a class file that extends Application and declare the variable in it.  In the other files, the variable can be accessed as follows:
Variable1 = ((class file name) getApplication()). Variable name.

9.
How to make the screen or part of the screen scrollable?
Wrap the layout to be scrolled in a ScrollView using <ScrollView> tag. Remember to have just one layout (like RelativeLayout or LinearLayout) or control (like TextView or Button) inside the ScrollView.
10.
How to display HTML text in a button?
Button.setText(Html.fromHtml(sText)) where sText is the string containing the text in HTML format.




Below is the list of some frequent issues encountered while testing an android application.

S.No
Issue
Solution
1.
NumberFormatException
This may occur due to several reasons and one of the reasons is setting the value of an EditText control with inputType as signed decimal, to spaces.
For ex: If an EditText control has inputType defined as below.
android:inputType=”numberDecimal/numberSigned”

If you set the value of the above EditText to spaces as below
mEditText.setText(“ “), it will throw a NumberFormatException.
Set the EditText control’s inputType to "numberSigned" only if you need the control to accept negative value, otherwise set it to just "numberDecimal".


2.
‘Window leaked error message’ when using an AlertDialog.
This error message will be thrown if the dialog is not dismissed (Dialog.dismiss()) in the onClick() method.

3.
WARNING: Application does not specify an API level requirement!
In the Android Manifest file set the minimum SDK version required as below.
<uses-sdk android:minSdkVersion=”3”/>