0

So I'm wondering how one could implement to an action bar with a logo button, a different image per state.(pressed, released etc).

I know how to do this for a normal button, but I'm not sure if it's possible with the actionbars logo button?

And if it's not, how could one implement am action bar that supports this? externals libs?

Thank you.

rennoDeniro
  • 920
  • 2
  • 8
  • 19

3 Answers3

3

Create a drawable xml file in res/drawable such as res/drawable/button_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed"
      android:state_pressed="true" />
    <item android:drawable="@drawable/button_normal" />
</selector>

where button_pressed and button_normalare just regular png files in one of your drawable directories (e.g/ drawables-hdpi)

Then in your menu.xml or in your code you can refer to @drawable/button_selector or R.drawable.button_selector and the image will change automatically when the button is pressed.

If you need to change the ActionBar button background you need to override the theme in your res/values/style.xml.

<style name="YourTheme.Sherlock" parent="@style/Theme.Sherlock">
    <item name="actionBarItemBackground">@drawable/actionbar_item_background_selector</item>
    <item name="android:actionBarItemBackground">@drawable/actionbar_item_background_selector</item>
</style>

Both lines matter: one for the SherlockActionBar and one for the standard ActionBar

Then set the theme to your activity in the manifest:

   <activity
        android:name=".yourActivity"
        android:theme="@style/YourTheme.Sherlock" >
    </activity>

And here is actionbar_item_background_selector.xml to be placed in res/drawable

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Even though these two point to the same resource, have two states so the drawable     will invalidate itself when coming out of pressed state. -->
    <item android:state_focused="true"  android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/scene_overlay_bar_pressed_center"     />
    <item android:state_focused="true"  android:state_enabled="false"                                  android:drawable="@drawable/abs__list_selector_disabled_holo_light" />
    <item android:state_focused="true"                                android:state_pressed="true" android:drawable="@drawable/scene_overlay_bar_pressed_center" />
    <item android:state_focused="false"                               android:state_pressed="true" android:drawable="@drawable/scene_overlay_bar_pressed_center" />
    <item android:state_focused="true"                                                             android:drawable="@drawable/abs__list_focused_holo" />
    <item                                                                                          android:drawable="@android:color/transparent" />

</selector>

replace the background with the drawable of your choice where android:state_pressed="true"

It's a lot to do for just a background but that is how the ActionBar works.

znat
  • 13,144
  • 17
  • 71
  • 106
  • Hello, does this also replace the default blue highlight when pressing down? – rennoDeniro Dec 03 '12 at 19:15
  • There is more work to do to replace the background. See the edited answer – znat Dec 03 '12 at 19:40
  • I see youre referencing the Shelock bar, meaning I need to implement this third party lib. So im guessing there's no way to do it without the lib and just the standard action bar? – rennoDeniro Dec 03 '12 at 20:45
  • The ActionBar is only available for Android 3.0+. If you want backward compatibility you need ActionBarSherlock which ports the ActionBar to versions prior to Android 3.0. See http://actionbarsherlock.com/ – znat Dec 03 '12 at 20:47
  • So, if im only supporting 3.0+, I need not to implement actionbarsherlock, correct? – rennoDeniro Dec 03 '12 at 21:40
  • Correct. Then you just have to change the theme @style/Theme.Sherlock to the one you are actually overriding. One of the lines will not be necessary but I don't remember which one. – znat Dec 03 '12 at 21:42
1

you can use a selector to pick different image. Android ImageButton with disabled UI feel may be help

Community
  • 1
  • 1
xff1874
  • 336
  • 1
  • 4
  • 9
0

I think you are looking for this:

final ActionBar bar = getActionBar();
bar.setDisplayHomeAsUpEnabled(true);

This automatically makes the logo you have set look like its being pressed when its clicked. No need to have different images to show state change. Thats a pretty easy way. Though if you are using ActionBarSherlock this won't work, I think. Strictly 4.0 I believe. Check out the doc on ActionBar.

Andy
  • 10,553
  • 21
  • 75
  • 125