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”/>






Sunday, 25 December 2011

A few simple ways to manage "Low on space" on HTC Desire

HTC Desire is a great Android phone providing a rich user experience with it's Sense UI. It runs smooth and fast. Several cool features made me love this phone at first sight. That was until I get the "Low on space" notification. HTC was so stingy that it had included just 576 MB of ROM of which around 430 MB is consumed by Android, HTC Sense and HTC's bundled apps. This leaves just around 140 MB of space for the user to use for installing his downloaded apps. This is very meagre and soon every HTC Desire owner hits the dreaded "Low on space" notification. This handicaps the expandability of the phone which is the core strength of any Android phone. Nevertheless I still love my phone. I had come up with a few techniques to "manage" this problem as shown below.

  • Move to SD: The first and the most obvious choice is to move apps to SD card. It's not possible to move every app to SD. Also, the apps for which there is a widget available and you use it, should not be moved to SD card.
  • Contacts Storage: Contacts storage may increase from around 10Mb to 80Mb, due to facebook contacts. Hence don't add facebook contacts to phone contacts (People), though you can link them. 
  • Clear Cache: Cache of several applications (like Market) can increase over time. Clear cache by selecting the app in Settings->"Manage Applications"->"Downloaded" tab and clicking the "Clear Cache" button. The easier way to do is by using the app Android System Cleaner - Cache Clean tab.
  • Low storage Apps: The beauty of Android is, there are several apps offering the same functionality and hence the user has a lot of freedom to pick and choose. Hence you can always find an app for a particular functionality that uses lesser storage than the one that you currently have on your phone. Look out for such apps and replace your current one. (I replaced Aldiko with Moon-reader and Rock Player with Mobo player, on this principle)
  • Adobe Flash: Adobe Flash (updates) normally consumes around 10MB of space. Uninstall the updates, you will not loose much on the functionality.
  • Internet Cache: Keep your stock Internet App's cache size at a minimum. I keep it at 2 MB.  You can do this by launching the internet app, choose settings and then selecting cache size. This is internal cache and is different from app cache. App cache size is over and above this setting, which we cannot control. Any amount less than 2 MB may increase the app cache size to mammoth proportions.
  • App Data: Periodically, you can delete all the data in social networking apps like, facebook, linked in/droidin etc. To do this,go to Settings->"Manage Applications". Click on those apps and click "Clear Data". The only trouble is, when you launch them now you will be prompted to enter your userid and password again.
  • Uninstall Apps: Finally, if you are not using any of your apps, uninstall them.

Thursday, 8 December 2011

Android - ListView without ListActivity


ListView is easier to use and straight forward if we extend ListActivity. But what happens if the activity already extends some other activity. Unfortunately , we do not have an option to extend two activities.

No issues, we could still create our customized list without extending ListActivity.

In the below example 'selectprofile.xml', a list called profilelist is declared as below.








<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@color/bgcolor">
    

<TextView 
 android:id="@+id/header"
 android:layout_width="fill_parent"
 android:layout_height="50dp"
 android:textColor="@color/headtxtcolor"
 android:text="Select Profile"
 android:textSize="20dp"
 android:textStyle="bold"
 android:gravity="center"
 android:layout_gravity="center_horizontal"
 android:background="@drawable/button_image"
/>


<ListView 
android:id="@+id/profilelist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:layout_weight="1"
android:dividerHeight="1dp">
</ListView>


</LinearLayout>


In the code

 static final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();

 public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.selectprofile);
       ...
       ...
       initializeList();
       getProfData();
       lv.setAdapter(adapter);
}

protected void initializeList(){
    
    
     lv = (ListView)findViewById(R.id.profilelist);
    
     adapter = new SimpleAdapter(this,list,
  R.layout.selectprofile_row,
   new String[] {"id","name","sex","year","date"},     
 new int[]{R.id.profid,R.id.name,R.id.sex,R.id.year,R.id.crdatevalue}
   );

  list.clear();    
 
}

Here 'selectprofile_row' defines the layout of the list row.
After populating the array list in getProfData() , set adapter to the list view. You can also add listener to the list item , if needed.