Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

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.

Android - Generic declaration of colours


It is very important that we declare background colours and layouts generically. I made this mistake of hardcoding the colours in the individual xml files. It became really hectic every time I wanted to try different colours. Then finally, I decided to declare them generically. Here is how you do it.

Declare mycustomcolors.xml in the values folder. Define all the colour codes here.


<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="bgcolor">#F8F8F8</color>
<color name="headtxtcolor">#e69b28</color>
<color name="othertxtcolor">#000000</color>
<color name="headerEnd">#999999</color>
<color name="headerStart">#000000</color>
<color name="profileEnd">#E69B28</color>
<color name="profileStart">#FFAE33</color>
<color name="sectionEnd">#FFFFFF</color>
<color name="sectionStart">#CCCCCC</color>
</resources>

   
The above colour codes can be used in shapes and other layouts. This is for a gradient colour button profile_band.xml in drawable-hdpi. 

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

<gradient
    android:startColor="@color/profileStart"
    android:endColor="@color/profileEnd"    
    android:angle="90" /> 
    
</shape>
Text colours used in an xml..

 <TextView
        android:id="@+id/profName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/header"
        android:paddingLeft="10dp"
        android:text="profile name"
        android:textColor="@color/othertxtcolor"
        android:textSize="14dp"
        android:layout_gravity="center_vertical"
       />


As mobile apps should have good screen appearance , it is very important that you have the flexibility to try various options.