5

I have a split action bar, where the top bar is dark and the bottom bar (split) is light.

Consequently, I'd like to show a contrast action icons: Light icons in the top dark bar and dark icons in the bottom light bar.

The problem is knowing if the actions should be painted on the top or bottom bar. How can I know that?

Another option is to know whether the action bar is currently split. How do I know that?

Thanks.

AlikElzin-kilaka
  • 34,335
  • 35
  • 194
  • 277
  • There's a difficulty doing split with contrast bars, because of the overflow icon. Action icons can be changed, but the overflow cannot. See question: http://stackoverflow.com/questions/13934825/how-to-change-the-overflow-button-when-in-cab – AlikElzin-kilaka Jan 04 '13 at 00:27

2 Answers2

8

Simple. You use boolean values. By default you'll have a split ActionBar if the screen width is smaller than 400dp. So in your values folder you can put:

/values/bools.xml:

<resources>
    <bool name="split_action_bar">true</bool>
</resources>

and in your values-sw400dp you put the following.

/values-sw400dp/bools.xml:

<resources>
    <bool name="split_action_bar">false</bool>
</resources>

Now you can set your icon based on that value:

boolean isActionBarSplitted = getResources().getBoolean(R.bool.split_action_bar);
if(isActionBarSplitted){
      // set light icon
}
else{
     // set dark icon
}

Edit:

Actually forget what I wrote, you don't need to create your own boolean value to check it. There is already one declared(which is the one the ActionBar uses to determine if it is a handset device or a tablet). If you're targeting Android HC+, then you can access the default ActionBars value: android.R.bool.split_action_bar_is_narrow, if you are using ActionBarSherlock: R.bool.abs_split_action_bar_is_narrow. Found here for the default ActionBar, here your ABS.

Ahmad
  • 69,608
  • 17
  • 111
  • 137
  • 1
    Thanks. How do you know it's 400dp? – AlikElzin-kilaka Jan 04 '13 at 06:24
  • Wow, great. Can you please share the name of the class and method? – AlikElzin-kilaka Jan 04 '13 at 06:38
  • Thanks but "android.R.bool." doesn't offer anything. When I type "android.R.bool.split_action_bar_is_narrow, i get a compilation error. See screenshot: http://ppl.ug/uQE38Eg4ONc/ – AlikElzin-kilaka Jan 06 '13 at 08:50
  • I found something. It seems that split_action_bar_is_narrow is internal: com.android.internal.R.bool.split_action_bar_is_narrow. Dead end? – AlikElzin-kilaka Jan 06 '13 at 09:21
  • 1
    Hmm, I did not know that this wasn't part of the public API. I tested it out with the ActionBarSherlock boolean and that did work. Try this here: http://stackoverflow.com/a/8683466/1333975 – Ahmad Jan 06 '13 at 15:02
  • Did the following and it worked: int id = Resources.getSystem().getIdentifier("split_action_bar_is_narrow", "bool", "android"); boolean isSplit = getResources().getBoolean(id); – AlikElzin-kilaka Jan 07 '13 at 12:05
  • The problem is that now I don't know if this identifier exists in all Android 4+. I found this identifier was added on April 2011 (https://github.com/android/platform_frameworks_base/commit/9b4bee0f14bbd137b0797127aff2df46a6321ec5). Is this identifier part of every Android 4+??? – AlikElzin-kilaka Jan 07 '13 at 12:09
  • 2
    @kilaka If you're using ABS, you might want to use the compatibility method `getResources_getBoolean(mActivity, R.bool.abs__split_action_bar_is_narrow);`, since the qualifier used by this resource (w480dp) is not directly supported by Android versions older than 3.2, therefore, it would return always the default value, `true`. – mdelolmo May 08 '13 at 08:53
0

It depends on the library you are using.

For the new Appcompat : check the value of R.bool.abc_split_action_bar_is_narrow:

boolean split = getResources().getBoolean(R.bool.abc_split_action_bar_is_narrow);

For ActionBarShelock : import the ResourcesCompat class from the library and call :

boolean split = ResourcesCompat.getResources_getBoolean(this, R.bool.abs__split_action_bar_is_narrow);

If you don't use any library then you can go with Ahmad's answer but only if you don't use a library. This is because they use different values. The android framework seems to split the bar if the width is lower than 400dp, but for appcompat and ABS it's 480dp.

Dalmas
  • 26,409
  • 9
  • 67
  • 80