SaltyNuts
SaltyNuts

Reputation: 5168

Android ActionBar styling issues

I want to have custom background for the menu items displayed in the ActionBar. I found out that I can do that by adding the following item to my app theme style definition:

    <item name="android:actionBarItemBackground">@drawable/actionbar_button</item>

Where actionbar_button.xml contains:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/action_btn_on" android:state_pressed="true" />
    <item android:drawable="@drawable/action_btn_off" />
</selector>

However, this causes the background image to also render behind the custom app logo displayed on the left of the ActionBar. After several attempts at figuring out a selector that would include only the buttons, I came up with this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/action_btn_on" android:state_pressed="true" android:state_enabled="true" />
    <item android:drawable="@drawable/action_btn_off" android:state_pressed="false" android:state_enabled="true"/>
    <item android:drawable="@android:color/transparent"/>
</selector>

Which sort of worked, and after the app loads fully the background behind the logo is transparent as I want it. But the problem is that while it's still loading it briefly shows the logo with the button background behind it and more annoyingly, it scales and crops the logo to fit inside the fill area of the 9-patch graphic that is the button background. When finished loading, the background is switched out, but the scaling and cropping remains, which looks quite ugly. How can I fix it?

Upvotes: 0

Views: 281

Answers (1)

SaltyNuts
SaltyNuts

Reputation: 5168

Have not found a working solution to the general problem of setting image-based backgrounds to ActionBar buttons, but the scaling/cropping does not happen if I use entered XML based backgrounds, such as created with a layer-list of shapes. For some cases in may not be viable, but in my case it was entirely possible, and maybe even better than using a bitmap based resource.

Upvotes: 1

Related Questions