Change actionbar button background color when pressed

If I press a button in the action bar, then its background color is not what I want. The background color of my item doesn't respond to my click event. How can I change this and change the background color when it's pressed?


You need to declare android:actionBarItemBackground attribute which is a:

Custom item state list drawable background for action bar items.

Then, in your styles do as follows:

<style name="CustomStyle" parent="@style/Theme.Holo.Light" >
    <item name="android:actionBarItemBackground">@drawable/ab_item_background</item>
    <item name="actionBarItemBackground">@drawable/ab_item_background</item>

So, put your own drawable with a selector and every state (pressed, focused, disabled, etc) to have the expected background. For example, the drawable ab_item_background.xml declared above might be like this:

<selector xmlns:android="" 
    <!-- focused/pressed: color=red -->
        android:drawable="@color/red" />
    <!-- pressed: color=red -->
        android:drawable="@color/red" />
    <!-- normal: color=transparent -->
        android:drawable="@android:color/transparent" />

In Styling the Action Bar, you can find all the customization possibles and all the attributes to do so.

ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0a0a0a")));

this might help

Need Your Help

upload a image contained in a url to Facebook in android

android facebook image upload

I used the following code, to upload a image to my Facebook wall.

How to get ALAsset aspectRatioThumbnail size in advance?

ios alasset

In ios, Using ALAsset, how can I get ALAsset aspectRatioThumbnail size (height and width) in advance (without loading the image into some UIImageview). I know I can get the full image's size using ...