Home » Android » android – How to set custom color on selected/highlighted list-item in activatedBackgroundIndicator

android – How to set custom color on selected/highlighted list-item in activatedBackgroundIndicator

Posted by: admin June 15, 2020 Leave a comment

Questions:

Hello I am developing an app using ActionBarSherlock and Navigation Drawer. I have created an initial navigation drawer with actionBar Sherlock just like this:

activatedBackgroundIndicator

Everything seems okay But I want to change default blue color of highlighted list-item with my custom color. I tried this link but it is not working. What I need to do to achieve custom highlight color?

How to&Answers:

Hello I have solved it for android OS version 11 and higher by applying style:

<style name="AppBaseTheme" parent="Theme.Sherlock.Light.DarkActionBar">
    <!-- API 14 theme customizations can go here. -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:activatedBackgroundIndicator">@drawable/activated_background</item>
</style>

activated_background in the drawable folder:

<selector xmlns:android="http://schemas.android.com/apk/res/android">  
   <item android:state_activated="true" android:drawable="@color/uva_color" />
   <item android:state_selected="true" android:drawable="@color/uva_color" />
   <item android:state_pressed="true" android:drawable="@color/uva_color" />
   <item android:drawable="@color/default_color" />  
</selector>

Color values in the values folder:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="uva_color">#c31756</color>
    <color name="default_color">#111</color>
</resources>

And set choicemode in the listview:

android:choiceMode="singleChoice"

And at last, send getBaseContext() to arrayApdater/your customAdapter as parameter instead of getApplicationContext().

mMenuAdapter = new MenuListAdapter(this.getBaseContext(), title, icon);

Also this link would help you.

Comment below if you face problem anywhere. I am ready to answer anytime, I don’t want to let anyone getting trouble with this like me. Cheers!