Thursday, February 21, 2019

Set background ListView

We want to set bacground for ListView like this.

Create a xml file name back.xml in folder drawable.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#EFEFEF"
        android:endColor="#989898"
        android:type="linear"
        android:angle="270"
    />
</shape>
In folder layout, create a xml file name li2.xml to set color string.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="42sp" 
    android:background="@drawable/back" 
    android:orientation="horizontal" > 
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5sp"       
        android:textColor="#0000aa"
        android:textSize="18sp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:contentDescription="@null"
        android:src="@drawable/ten" />

</RelativeLayout>
Set adapter for ListView
adapter=new ArrayAdapter<String>(this,R.layout.li2,R.id.text, array);


Run to see the result.

No comments:

Post a Comment