Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get ActionBar attribute in my class [android]

Simple question how do I use getDisplayOptions() of action bar? I want to maintain the state of action bar as I am flipping my view with some custom view. So before bringing my view I am storing my action bar state, so that I revert back to original state when my view is removed. I set few setting to my action bar like

    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setDisplayShowCustomEnabled(false);
    actionBar.setDisplayShowHomeEnabled(true);

So when I remove my view I want to revert back these setting. But I don't see any method like actionBar.isDisplayHomeAsUpEnabled() etc in API. Although I see actionBar.getDisplayOptions(). But don't know how to use it. Can any body help me achieving this.?

like image 255
Android Avatar asked Nov 07 '14 05:11

Android


1 Answers

Simple question how do I use getDisplayOptions() of action bar?

Whenever you change the display options in the ActionBar, ActionBar.setDisplayOptions(int options, int mask) is called internally to pair that display option with the corresponding bitmask.

For instance, when ActionBar.setDisplayHomeAsUpEnabled is called, internally ActionBar.setDisplayOptions(int options, int mask) is called like this:

setDisplayOptions(showHomeAsUp ? DISPLAY_HOME_AS_UP : 0, DISPLAY_HOME_AS_UP);

So, if you want to check to see if particular display option is enabled, you just compare that mask to 0 using a bitwise operation.

// Retrieve the current set of display options
final int displayOptions = actionBar.getDisplayOptions();
// Determine which display options are enabled
final boolean isShowHomeEnabled = (displayOptions & ActionBar.DISPLAY_SHOW_HOME) != 0;
final boolean isHomeAsUpEnabled = (displayOptions & ActionBar.DISPLAY_HOME_AS_UP) != 0;
final boolean isShowTitleEnabled = (displayOptions & ActionBar.DISPLAY_SHOW_TITLE) != 0;
final boolean isUseLogoEnabled = (displayOptions & ActionBar.DISPLAY_USE_LOGO) != 0;
final boolean isShowCustomEnabled = (displayOptions & ActionBar.DISPLAY_SHOW_CUSTOM) != 0;

A complete example might be something like:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayUseLogoEnabled(true);

    // Retrieve the current set of display options
    final int displayOptions = actionBar.getDisplayOptions();
    // Determine which display options are enabled
    final boolean isShowHomeEnabled = (displayOptions & ActionBar.DISPLAY_SHOW_HOME) != 0;
    final boolean isHomeAsUpEnabled = (displayOptions & ActionBar.DISPLAY_HOME_AS_UP) != 0;
    final boolean isShowTitleEnabled = (displayOptions & ActionBar.DISPLAY_SHOW_TITLE) != 0;
    final boolean isUseLogoEnabled = (displayOptions & ActionBar.DISPLAY_USE_LOGO) != 0;
    final boolean isShowCustomEnabled = (displayOptions & ActionBar.DISPLAY_SHOW_CUSTOM) != 0;

    System.out.println("show home: " + isShowHomeEnabled);
    System.out.println("home as up: " + isHomeAsUpEnabled);
    System.out.println("show title: " + isShowTitleEnabled);
    System.out.println("use logo: " + isUseLogoEnabled);
    System.out.println("show custom: " + isShowCustomEnabled);
}

Which would print:

show home: false
home as up: true
show title: false
use logo: true
show custom: false
like image 154
adneal Avatar answered Sep 18 '22 00:09

adneal