Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if an Android actionbar's action icon is on the top bar or the bottom bar (split)?

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.

like image 423
AlikElzin-kilaka Avatar asked Jan 04 '13 00:01

AlikElzin-kilaka


People also ask

What is the action bar on an Android phone?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.

What are action bar icons?

The ActionBar, now known as the App Bar, is a consistent navigation element that is standard throughout modern Android applications. The ActionBar can consist of: An application icon. An "upward" navigation to logical parent.

How do you add action items to the action bar in Android?

All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.

What is support action bar?

In its most basic form, the action bar displays the title for the activity on one side and an overflow menu on the other. Even in this simple form, the app bar provides useful information to the users, and helps to give Android apps a consistent look and feel.


1 Answers

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.

like image 162
Ahmad Avatar answered Oct 15 '22 03:10

Ahmad