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>
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With