4

Hi I am using listview which have multiple items adding dynamically.. I want to change the text color of listitem any idea

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
       android:layout_height="wrap_content"
       android:background="#659EC7">

    <ImageView android:id="@+id/icon" android:layout_width="72px"
        android:layout_height="wrap_content"
                android:layout_marginTop="5px" />

    <TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
        android:orientation="vertical" 
                android:paddingTop="5px" 
                android:paddingBottom="5px">

        <TextView android:id="@+id/item1" android:layout_width="match_parent"
            android:layout_height="wrap_content" 
                         android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView android:id="@+id/item2" android:layout_width="match_parent"
            android:layout_height="wrap_content" 
                       android:textAppearance="?android:attr/textAppearanceSmall"
            android:paddingTop="30px" />
            <View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray"/>

    </TwoLineListItem>

</LinearLayout>

I am using 2linelistIem

Sumit
  • 928
  • 2
  • 15
  • 34

2 Answers2

1

Use this type of technique to change the color dynamically:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@color/black" /> <!-- focused -->
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/black" /> <!-- focused and pressed-->
    <item android:state_pressed="true" android:drawable="@color/green" /> <!-- pressed -->
    <item android:drawable="@color/black" /> <!-- default -->
</selector> 
Soumyadip Das
  • 1,781
  • 3
  • 16
  • 34
1

Here is the code I wrote and used: It keeps the android style for all other states. it just overlays the transparent state with your desired color:

Layer list drawable for list view with custom background

as you can see in my question there is a minor bug with the focused state, but it is still the best solution i ever saw :D

Community
  • 1
  • 1
Diego Frehner
  • 2,396
  • 1
  • 28
  • 35