Skip to content

biegleux/ActionBarCompat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

ActionBarCompat

ActionBar is a design pattern introduced in API 11. To support older versions of Android Google provides Action Bar Compatibility sample app.

This library project extends the functionality of the provided ActionBarCompat to support more APIs.

Based on:

Usage

Setting action bar properties

If you want to set action bar properties in onCreate(), you need to initialize action bar first by calling getActionBarHelper().initActionBar(). This initializes action bar for pre-Honeycomb devices, does nothing for later API levels.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    getActionBarHelper().initActionBar();
    ActionBarCompat.setDisplayShowHomeEnabled(this, true);
    ...

Another option is to set action bar properties in onPostCreate(), this way there is no need to call getActionBarHelper().initActionBar() as action bar will be already initialized in this stage.

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    ActionBarCompat.setDisplayShowTitleEnabled(this, true);
    ActionBarCompat.setTitle(this, R.string.actionbar_custom_title);
    ...
}

Accessing action views

You can access action view by calling getActionView().

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.options, menu);
    if (mMenu != null) {
        return super.onCreateOptionsMenu(menu);
    }
    mMenu = menu;
    MenuItem item = mMenu.findItem(R.id.menu_search);
    EditText editText = (EditText) MenuItemCompat.getActionView(item);
    ...

Note: MenuItemCompat has to be of type sk.m217.actionbarcompat.MenuItemCompat instead of type android.support.v4.view.MenuItemCompat.

Handling collapsible action views

You can handle expanding and collapsing of action view by defining an MenuItemCompat.OnActionExpandListener and registering it with setOnActionExpandListener().

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.options, menu);
    if (mMenu != null) {
        return super.onCreateOptionsMenu(menu);
    }
    mMenu = menu;
    MenuItem item = mMenu.findItem(R.id.menu_search);
    if (item != null) {
        MenuItemCompat.setOnActionExpandListener(item,
                new MenuItemCompat.OnActionExpandListenerCompat() {
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                // Do something when collapsed
                return true;
            }
            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                // Do something when expanded
                return true;
            }
        });
    }
    ...

See sample application for more details how to use ActionBarCompat library.

Supported APIs

Collapsible action views are not supported for Honeycomb devices.

XML attributes support:

android:showAsAction="never|always|collapseActionView"
android:actionLayout

Supported ActionBar APIs:

Supported MenuItem APIs:

About

Action Bar Compatibility Library

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages