Updated 22 December 2016
In this blog,
I have shown the method to change the background Color of the ListFragment on the basis of the current states of the list using custom Selector(Color State List Resource).
First of all, I will give the small introduction about the selector.
Actually, Selector is nothing but the XML element <selector>
which is used to describe the state list in an XML file.
When state change, the state list is traversed top to bottom and the first item that matches the current state will be used.
SYNTAX:
1 2 3 4 5 6 7 8 9 10 11 12 |
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:color="hex_color" android:state_pressed=["true" | "false"] android:state_focused=["true" | "false"] android:state_selected=["true" | "false"] android:state_checkable=["true" | "false"] android:state_checked=["true" | "false"] android:state_enabled=["true" | "false"] android:state_window_focused=["true" | "false"] /> </selector> |
You can make it into the drawable package.
<selector>: This must be the root element. Contains one or more <item> elements.
attributes:
<item>:
Defines a color to use for certain states, as described by its attributes. Must be a child of a<selector> element.
attributes:
If you want the default selector in ListFragment then assign it as your row layout background.
1 |
android:background="?android:attr/activatedBackgroundIndicator" |
Or create our selector file
Step 1: Create a selector file
list_selector.xml
1 2 3 4 5 6 7 8 9 10 11 12 |
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_longAnimTime"> <item android:drawable="@color/accent_color" android:state_pressed="true" /> <!--<item android:drawable="@color/dark_primary_color" android:state_focused="true" android:state_selected="true" />--> <!-- Selected --> <item android:drawable="@drawable/focused" android:state_focused="true" /> <!-- Pressed --> <item android:drawable="@drawable/selected" android:state_selected="true" /> </selector> |
Step 2: Apply it on to the List or ListFragment
Use setSelector(int) for apply selector on the list
If you want to apply this on to the ListView Simply write
1 2 3 |
ListView listView = (ListView) findViewById(R.id.listView); listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); listView.setSelector(R.drawable.list_selector); |
OR If you want to apply this on to the ListFragment Simply write
1 2 3 4 5 6 7 |
public void setActivateOnItemClick(boolean activateOnItemClick) { // When setting CHOICE_MODE_SINGLE, ListView will automatically // give items the 'activated' state when touched. // Toast.makeText(getActivity(), "setActivateOnItemClick", Toast.LENGTH_SHORT).show(); getListView().setChoiceMode(activateOnItemClick ? ListView.CHOICE_MODE_SINGLE : ListView.CHOICE_MODE_NONE); getListView().setSelector(R.drawable.list_selector); } |
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.