Thursday, 31 March 2011

Custmise ListView

To make an customise List View in Android  You have to make a new adepter class  extend Array Adapter or Base Adapter and  @Override  the getView and specify your required layout to list to be return by this method and use this adapter in our list view.





 







frist make required layout in xml like

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/continent_search" android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip" >
   
    <ImageView android:id="@+id/icon" android:layout_width="70dp"
        android:layout_height="70dp" android:layout_marginRight="6dip" android:scaleType="fitXY"
        />
   
    <LinearLayout android:padding="10dip"
        android:orientation="vertical" android:layout_width="0dip"
        android:layout_weight="1" android:layout_height="fill_parent">
        <TextView android:id="@+id/toptext" android:layout_width="fill_parent"
            android:layout_height="0dip" android:layout_weight="1"
            android:gravity="center_vertical" android:textColor="@drawable/white"
              android:text=""/>
        <TextView android:layout_width="fill_parent" android:ellipsize="end"
            android:layout_height="0dip" android:layout_weight="1"                    android:id="@+id/bottomtext"
android:text="" android:singleLine="true"
            android:textColor="@drawable/white"  />
    </LinearLayout>

</LinearLayout>    


then make an adepter class

class Name_Adapter extends ArrayAdapter<UR_Adapter_Type> {
       

        public Name_Adapter(Context context, int textViewResourceId,
                UR_Adapter_Type objects) {
            super(context, textViewResourceId, objects);

           // Do your stuff if what you needed.....

        }

        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
        
  // Make Your view Here By using  Layout inflate  like ....

LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = convertView;
            v = vi.inflate(R.layout.UR_XML_NAME, null);
//Getting View  Element

ImageView iv = (ImageView) v.findViewById(R.id.icon);
            TextView t1 = (TextView) v.findViewById(R.id.toptext);
            TextView t2 = (TextView) v.findViewById(R.id.bottomtext);
//  set  your data to view  like image src text values etc .
                    .
                    .
                    .
                    .
// return your view 
   return v;
          
        }
    }
}


Now in Activity onCreate method get list view refrence and make adapter object and  and set to List View  then you get requried output

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cakemenu);
        ListView li = (ListView) findViewById(R.id.listview);
  // make adapter object

    Name_Adapter      m = new Name_Adapter(this, R.layout.<layout_for_list>,       UR_Adapter_Type_object);
       li.setAdapter(m);



}











Starting Android

After Downloading of android SDK (If not then Click here )and Installing  ADT pluggin for eclipse(If not then  Click here ) now we write our frist android app. if preference not to android then  start eclipse then click on Windows --> Preference --> Android  after that Bowes SDK location .





After that Update SDK click Windows -->Android SDK and AVD manager --> Available package then install Android Repository SDK platform like  SDK Platform  Android 2.2.3 API 10 


 It's time to create an Virtual Device in Android SDK and AVD manager go to Virtual device Pane and Click on Add new pop-up  open enter name of AVD set Target give SD card size in MB Change skin default to HVGA Create AVD.
Now we have new AVD select it and Start it .
Close Android SDK and AVD manager.
 We are ready for first application , click on File -->New --> Other/Android Project

Specify project name ,target,application name,pkg name, check Create Activity give its name Min sdk suppose 10 etc. then Finish Your first project almost ready..

Open your Activity class which looks like ..


package pkg_name;

import android.app.Activity;
import android.os.Bundle;

public class Activity_Name extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

here Class which extends Activity (android.app.Activity) ,Activity provide instruction to user it handle all UI.
setContentView(R.Layout.main); View layout for activity define in main.xml at res > layout folder main.xml looks like   

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
     
</LinearLayout>    


Where in linear layout one Text Box show value of hell string from string.xml (res > values > string.xml )


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, TestFrist!</string>
    <string name="app_name">Frist Project</string>
</resources>

Add one more folder drawable  in res folder of Package explorer  by right clicking,Add one image or paste one image in this folder (image name must be small letter ) like loksa2.jpg (prefer .png format)  and in main.xml replace <TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
with
<ImageView android:layout_width="fill_parent" android:id="@+id/imageView1"
        android:layout_height="fill_parent" android:src="@drawable/loksa2"></ImageView>



  Now Right click on project at Project Explorer and choose Run As --> Android Application .  You see the output like this ..........