简体   繁体   中英

How to change Listview item color and textcolor in android

In my app I have created one Listview. When I click on the row, I want to change the Listview row color as Lightgray color and textcolor as a Blue color.

For this I have tried the code below but only row background color is changing not the textcolor.

mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                for (int j = 0; j < parent.getChildCount(); j++) {

                    parent.getChildAt(j).setBackgroundColor(Color.TRANSPARENT);
                    view.setBackgroundColor(Color.LTGRAY);

                    rowText = (TextView) findViewById(R.id.rowitem);
                    rowText.setTextColor(Color.BLUE);
                }


   }
    });
}

First create a selector drawable like this :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/red"
          android:state_pressed="true" />
    <item android:drawable="@android:color/white" />
</selector>

Then

StateListDrawable selector = new StateListDrawable();
selector.addState(new int[] { android.R.attr.state_pressed }, getResources().getDrawable(R.color.red));
selector.addState(new int[] {}, getResources().getDrawable(R.color.white));
view.setBackgroundDrawable(selector);

Change the

 rowText = (TextView) findViewById(R.id.rowitem);

to

rowText = (TextView)view.findViewById(R.id.rowitem);

since you should find the textview in the inflated view, not independently.

first enable the android:ListSelector attribute in your ListView

ListView android:id="@+id/android:list" android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_below="@+id/Tablayoutdesign"
    android:cacheColorHint="#000000"
    android:dividerHeight="1dip"
    android:layout_marginTop="63dip"
    android:layout_marginBottom="40dip"

    />

Thank create a selector (listselector.xml)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

  <!-- Selected --> 
  <item 
    android:state_focused="true" 
    android:state_selected="false" 
    android:drawable="@drawable/focused"/> 

  <!-- Pressed -->
  <item 
    android:state_selected="true" 
    android:state_focused="false"
    android:drawable="@drawable/selected" /> 

</selector> 

Than go for colors.xml

<resources>
<drawable name="focused">#ff5500</drawable>
<drawable name="selected">#FF00FF</drawable>
</resources>

and with some Java magic: listview.setSelector(R.drawable.listselector)

Thanks to @sankar-anesh

List State Pressed

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#4285f4" />
            <corners android:radius="2dp" />
        </shape>
    </item>

    <item
        android:bottom="2dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp">
        <shape android:shape="rectangle">
            <solid android:color="#4285f4" />
            <corners android:radius="2dp" />
        </shape>
    </item>
</layer-list>

List view background when not pressed

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#FFF" />
            <corners android:radius="2dp" />
        </shape>
    </item>

    <item
        android:bottom="2dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp">
        <shape android:shape="rectangle">
            <solid android:color="#FFF" />
            <corners android:radius="2dp" />
        </shape>
    </item>
</layer-list>

List view Selector

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/card_state_pressed" android:state_pressed="true" />
    <item android:drawable="@drawable/card_background" />
</selector>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM