Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define ColorStateList for TextView?

When my ListViewItem is highlighted, I want the text to turn white. How can I define this?

<selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_focused="true" android:color="@color/testcolor1"/>    <item android:state_pressed="true" android:state_enabled="false" android:color="@color/testcolor2" />    <item android:state_enabled="false" android:color="@color/testcolor3" />    <item android:color="@color/testcolor5"/>  </selector> 
like image 797
Sheehan Alam Avatar asked Sep 30 '10 03:09

Sheehan Alam


People also ask

What is ColorStateList in android?

A ColorStateList is an object you can define in XML that you can apply as a color, but will actually change colors, depending on the state of the View object to which it is applied.

How can we use custom colors in application?

Click New —> Values resource file menu item to popup the New Resource File dialog window. In the New Resource File dialog window, input keyword colors in the File name input box, select main in the Source set drop-down list, input values in the Directory name input text box, then click the OK button. Then colors.

What is a selector Android?

A selector may be created by invoking the open method of this class, which will use the system's default selector provider to create a new selector. A selector may also be created by invoking the openSelector method of a custom selector provider. A selector remains open until it is closed via its close method.


2 Answers

Create file res/drawable/text_color.xml:

 <selector xmlns:android="http://schemas.android.com/apk/res/android">     <item android:state_focused="true" android:state_pressed="false" android:color="#ffffff" />     <item android:state_focused="true" android:state_pressed="true" android:color="#ffffff" />     <item android:state_focused="false" android:state_pressed="true" android:color="#ffffff" />     <item android:color="#000000" /> </selector> 

Then use @drawable/text_color from xml (or R.drawable.text_color from code) as text color for your list view items.

like image 170
Konstantin Burov Avatar answered Oct 12 '22 22:10

Konstantin Burov


Try this...

First, create a color state list text_color.xml placed in res/color directory.

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"     tools:ignore="MissingDefaultResource">   <item android:color="#000000" android:state_enabled="false"/>   <item android:color="#FFFFFF"/> </selector> 

Second, use

getColorStateList(@NonNull Context context,             @ColorRes int id) 

method to get color state list.

textView.setTextColor(ContextCompat.getColorStateList(context, R.color.text_color)) 

Third, enable(true) or disable(false) based on your requirements,

textView.isEnabled = true //when item is highlighted 
like image 41
Silambarasan Poonguti Avatar answered Oct 12 '22 22:10

Silambarasan Poonguti