To make a custom ListView here we will use two XML layout.One is the main layout,where the ListView widget is taken and another is simple_list XML,where a TextView with a background.The ListView items will be shown above this TextView.So the code of main is:
And the code of simple_list.xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:padding="1dip" android:layout_height="wrap_content">
<TextView android:id="@+id/toptext" android:gravity="center_vertical" android:layout_height="wrap_content" android:padding="5dip" android:layout_width="fill_parent" android:textColor="#000000" android:background="@drawable/textfield" android:focusable="true"/>
</LinearLayout>
Here in the drawable folder of the project we take a image that is linked for the TextView background.In this project the image name is "textfield".(Remember,always give the image name a lower case name with no special character and image format should be png.).
Now it is time for the code.For this we take a array of string with different strings.(Here we use the states of USA).Now watch the code carefully:
package com.custom.list;
<?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"
/>
<ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/statelist"></ListView>
</LinearLayout>
<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"
/>
<ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/statelist"></ListView>
</LinearLayout>
And the code of simple_list.xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:padding="1dip" android:layout_height="wrap_content">
<TextView android:id="@+id/toptext" android:gravity="center_vertical" android:layout_height="wrap_content" android:padding="5dip" android:layout_width="fill_parent" android:textColor="#000000" android:background="@drawable/textfield" android:focusable="true"/>
</LinearLayout>
Here in the drawable folder of the project we take a image that is linked for the TextView background.In this project the image name is "textfield".(Remember,always give the image name a lower case name with no special character and image format should be png.).
Now it is time for the code.For this we take a array of string with different strings.(Here we use the states of USA).Now watch the code carefully:
package com.custom.list;