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?
2
- You got to customize you actionbar. Check out this two links Customizing ActionBar Custom ActionBarBri6ko– Bri6ko2014-05-13 19:49:53 +00:00Commented May 13, 2014 at 19:49
- i think you need this link too: jgilfelt.github.io/android-actionbarstylegeneratorhamedjj– hamedjj2014-05-13 20:08:38 +00:00Commented May 13, 2014 at 20:08
Add a comment |
2 Answers
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> </style> 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="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_mediumAnimTime"> <!-- focused/pressed: color=red --> <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/red" /> <!-- pressed: color=red --> <item android:state_pressed="true" android:drawable="@color/red" /> <!-- normal: color=transparent --> <item android:drawable="@android:color/transparent" /> </selector> In Styling the Action Bar, you can find all the customization possibles and all the attributes to do so.
5 Comments
MaartenDekkers
Fixed it by using DarkActionBar
Rohan Kandwal
Working perfectly, thanks.. Just one thing though
android:actionBarItemBackground is being understood by Android while actionBarItemBackground is giving error so I removed it, is it necessary to have both?Blo
It depends of your actionbar @RohanKandwal. If you use SherlockAB or the Support library, you have to use it, yes.
Rohan Kandwal
@Fllo I use Support library, as I said,
actionBarItemBackground gave error while compiling so I removed it. In this case, how can I use both.Blo
I see @RohanKandwal. When you write these attributes in style.xml, you have to close this file, clean and save your project.
ActionBar actionBar = getActionBar(); actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0a0a0a"))); this might help
1 Comment
Duyen Hang Kim
This is change background color for actionbar, not for home button