Reuse The Action Bar In All The Activities Of App
I am a newbie to android and I was wondering if someone could guide me about how to reuse the action bar in all of my android activities. As far as I have explored, I found out tha
Solution 1:
Well Your code looks good, but if you want to reuse exactly the same ActionBar with the same icons and menus and generally the same functionality in every activity.
You could add the code:
@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.menu, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_action_search).getActionView();
returntrue;
}
@OverridepublicbooleanonOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stubswitch(item.getItemId()){
case R.id.menu_action_search:
{}
case R.id.menu_action_locate:
{}
case R.id.menu_action_mail:
{}
case R.id.menu_action_call:
{}
}
returnsuper.onOptionsItemSelected(item);
}
in your BaseMenuActivity class and your actionbar will be populated the same for every activity that extends from it.
Update:
To create a menu layout you should create a folder 'menu' in your resources folder res/menu. Then create a xml file inside called : some_title.xml
A typical example of a menu xml file is like below:
<?xml version="1.0" encoding="utf-8"?><menuxmlns:android="http://schemas.android.com/apk/res/android" ><itemandroid:id="@+id/menu_search"android:actionViewClass="com.actionbarsherlock.widget.SearchView"android:icon="@drawable/abs__ic_search"android:showAsAction="ifRoom|withText|collapseActionView"android:title="@string/menu_action_search"/><itemandroid:id="@+id/menu_sort"android:icon="@drawable/content_sort_icon"android:showAsAction="always"android:title="@string/menu_action_sort"></item></menu>
and then inflate that file :
@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.some_title, menu);
SearchViewsearchView= (SearchView) menu.findItem(R.id.menu_action_search).getActionView();
returntrue;
}
For some more reading this tutorial is very very good on using ActionBar:
http://www.vogella.com/tutorials/AndroidActionBar/article.html
Post a Comment for "Reuse The Action Bar In All The Activities Of App"