here i found a very useful solution
http://www.michenux.net/android-listview-highlight-selected-item-387.html
http://www.michenux.net/android-listview-highlight-selected-item-387.html
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice">
</ListView>
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice">
</ListView>
Then, you need to create a selector. This is where you will configure colors for each defined state. The selected file
is in res/drawable directory.
is in res/drawable directory.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item android:drawable="@android:color/holo_orange_dark" android:state_pressed="true"/>
<item android:drawable="@android:color/holo_green_light" android:state_selected="true"/>
<item android:drawable="@android:color/holo_green_light" android:state_activated="true"/>
</selector>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item android:drawable="@android:color/holo_orange_dark" android:state_pressed="true"/>
<item android:drawable="@android:color/holo_green_light" android:state_selected="true"/>
<item android:drawable="@android:color/holo_green_light" android:state_activated="true"/>
</selector>
Then, on the item layout, add the attribute activatedBackgroundIndicator at top level:
<?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:orientation="horizontal"
android:background="?android:attr/activatedBackgroundIndicator">
<!-- your item content-->
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="?android:attr/activatedBackgroundIndicator">
<!-- your item content-->
</LinearLayout>
Finally, you need to link the selector with your ListView. This can be done in method onCreate of a ListActivity or
in method onActivityCreated of a ListFragment.
in method onActivityCreated of a ListFragment.
this.getListView().setSelector(R.drawable.your_selector);
That’s all.
Update (2013/05/30) :
I didnot explain how to change the blue color.
Here is the solution :
Here is the solution :
Create a file res/drawable/listitem_background.xml with the following content :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@color/purple_dark" />
<item android:drawable="@android:color/transparent" />
</selector>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@color/purple_dark" />
<item android:drawable="@android:color/transparent" />
</selector>
Replace the @color/purple_dark with the color of your choice.
Then, in your theme, add the following line :
<item name="android:activatedBackgroundIndicator">@drawable/listitem_background</item>
Comments
Post a Comment