Android – Custom background for activatedBackgroundIndicator on ActionBarSherlock doesn’t work

actionbarsherlockandroidandroid-layoutandroid-themebackground

I'm using ActionBarSherlock and I'm trying to customize the activatedBackgroundIndicator attribute for the row background.

If I use the latest android sdk, without the ActionBarSherlock, I'm able to customize the background creating the following style on res/values/style.xml and defining it on AndroidManifest.xml as android:theme="@style/Theme.Custom":

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="Theme.Custom" parent="android:Theme.Holo.Light.DarkActionBar">
     <item name="android:activatedBackgroundIndicator">@drawable/activated_background</item>
  </style>
</resources>

Then, my res/drawable/activated_background.xml contains the next code:

<selector xmlns:android="http://schemas.android.com/apk/res/android">  
   <item android:state_activated="true" android:drawable="@color/row_activated" />
   <item android:drawable="@android:color/transparent" />  
</selector>

Finally, the code used to define each row in the ListView is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:background="?android:attr/activatedBackgroundIndicator">
    <TextView
            android:id="@+id/test_row"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/sample_string"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:padding="15dp"/>
</LinearLayout>

The result is shown on the screenshoot. Is a simple application with only a ListView with ListView.CHOICE_MODE_SINGLE and getListView().setItemChecked(position, true) when list item clicked.

The blue color of the standard selected row now is yellow and works perfectly.

List without ABS

The problem appears when I want to apply the same customization using the ActionBarSherlock.
Now The style file is the next and the background appears blue instead the custom yellow.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme.Custom" parent="Theme.Sherlock.Light.DarkActionBar">
        <item name="activatedBackgroundIndicator">@drawable/activated_background</item>
        <item name="android:activatedBackgroundIndicator">@drawable/activated_background</item>
    </style>

    <style name="activatedBackgroundIndicator">
        <item name="android:background">?android:attr/activatedBackgroundIndicator</item>
    </style>
</resources>

List with ABS

I don't know if ActionBarSherlock supports the android:activatedBackgroundIndicator feature or if I forgot to implement something needed to be able to change the default color.

Any ideas?

Best Answer

Finally I found the solution to the problem.
It was the context used in the row adapter. The constructor used is the shown in the following piece of code:

public RowAdapter(Activity activity, ArrayList<String> list) {
    this.mContext = activity.getApplicationContext();
    this.elements = list;
    this.theActivity = activity;

    this.inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

Just changing the context from:
this.mContext = activity.getApplicationContext()(Application-Context)
to
this.mContext = activity.getBaseContext()(Activity-Context)
solved the problem and now the background is the custom one.

Whenever you need to manipulate Views then go for Activity-Context, else Application-Context would be enough.

This answer helped me to understand why to use getBaseContext() instead of getApplicationContext() in my case.

custom row background

Related Topic