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:
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);
...
}
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
.
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.
Collapsible action views are not supported for Honeycomb devices.
XML attributes support:
android:showAsAction="never|always|collapseActionView"
android:actionLayout
Supported ActionBar APIs:
- void setIcon(int)
- void setIcon(Drawable)
- void setTitle(CharSequence)
- void setTitle(int)
- void setDisplayShowHomeEnabled(boolean)
- void setDisplayHomeAsUpEnabled(boolean)
- void setDisplayShowTitleEnabled(boolean)
- void setBackgroundDrawable(Drawable)
- CharSequence getTitle()
- void setHomeButtonEnabled(boolean)
Supported MenuItem APIs:
- View getActionView()
- boolean expandActionView() (not supported for Honeycomb devices)
- boolean collapseActionView() (not supported for Honeycomb devices)
- boolean isActionViewExpanded() (not supported for Honeycomb devices)
- MenuItem setOnActionExpandListener(OnActionExpandListener) (not supported for Honeycomb devices)
- MenuItem setTitle(CharSequence)
- MenuItem setTitle(int)
- CharSequence getTitle()
- MenuItem setIcon(Drawable)
- MenuItem setIcon(int)
- Drawable getIcon()
- MenuItem setEnabled(boolean)
- boolean isEnabled()
- MenuItem setIntent(Intent)
- Intent getIntent()
- MenuItem setVisible(boolean)
- boolean isVisible()
- MenuItem setOnMenuItemClickListener(MenuItem.OnMenuItemClickListener)