Wednesday, February 20, 2019

Color string ListView, add image

If we want to change color ListView android, we can’t do it in source code xml file of ListView.
Create a xml file name li.xml, the code below.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="42sp"
    android:textColor="#000080"
    android:textSize="17sp"
    android:paddingLeft="10dp"/>

After that, set custom adapter for ListView like this.
adapter=new ArrayAdapter<String>(this,R.layout.li,R.id.text, qua);

Now ListView have color string as we want.

If we want ListView has an image at both side, copy two images name cuoi and ten in to drawable folder.


Add some lines in to file li.xml
<?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:orientation="horizontal" >

    <ImageView
        android:id="@+id/im"
        android:layout_width="40sp"
        android:layout_height="45sp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5sp"
        android:contentDescription="@null"
        android:src="@drawable/cuoi" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5sp"
        android:layout_toEndOf="@+id/im"
        android:layout_toRightOf="@+id/im"
        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>
Now our’s ListView will look like this


No comments:

Post a Comment