7

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 that we have to make a BaseActivity class and extend it in our Activity where we want to reuse it, and also we have to make a xml layout and include it in our activity xml file. I have finished with the BaseActivity part. Now I am sort of confused in framing the xml part and including it. I know how to merge and include a layout, But in case of Action Bar, what necessary steps are to be taken. Any help would be appreciated.

This is my BaseMenuActivity:

public class BaseMenuActivity extends Activity{

    ActionBar actionBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setIcon(R.drawable.ic_social_share);
        LayoutInflater inflator = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.apptitle, null);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setCustomView(v);
    }
}

Manifest part for the same:

<activity
            android:name="com.example.travelplanner.MenuActivity"
            android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden"
            android:uiOptions="splitActionBarWhenNarrow"
            android:label="WeTrip"
            android:theme="@style/MyTheme" >

Style.xml part:

<style name="MyTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>
    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">#F0F1F1</item>
        <item name="android:backgroundSplit">#000000</item>
    </style>

MenuActivity.java

public class MenuActivity extends BaseMenuActivity implements OnItemClickListener{

    ActionBar actionBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_menu);

    @Override
    public boolean onCreateOptionsMenu(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();
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch(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:
            {}
        }
        return super.onOptionsItemSelected(item);
    }

}
divyang7
  • 309
  • 3
  • 10
  • 22
  • so if I understand you correctly you just want to have the same actionbar through out your app? – tyczj Jul 26 '13 at 19:18
  • yes..but I dont know how to achieve it properly. Because I know I have to make a BaseActivity class and have to extend it, and also I have to create an xml file and include it in every activity I want to display the same actionbar.. but I need some guidance on it – divyang7 Jul 26 '13 at 19:24
  • you can just set the theme of the application in your manifest so then you dont have to set it for each individual activity in your manifest. You will however need to give it the view in each activity. If you dont want to do that then I suggest looking at switching fragments instead of activities – tyczj Jul 26 '13 at 19:25
  • Your code looks good. Did you try it? Is it not working? – Benito Bertoli Jul 26 '13 at 19:28
  • will the above code work...or it needs to be improved...and I have declared the theme part in style.xml..by setting the application theme, do you mean I should set the application theme as android:theme="@style/MyTheme" ? – divyang7 Jul 26 '13 at 19:31
  • No, I thought this is incomplete as I have not dealt with the xml layout. Some tutorials suggested to have a xml layout for the action bar and include it in the activity's xml file. I am not sure how to deal with it.. – divyang7 Jul 26 '13 at 19:33
  • @divyang7 yes in your application tag set `android:theme="@style/MyTheme"` then your whole application will take on that theme – tyczj Jul 26 '13 at 19:40

1 Answers1

10

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:

    @Override
    public boolean onCreateOptionsMenu(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();
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch(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:
            {}
        }
        return super.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"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android: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"/>
    <item
        android: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 :

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.some_title, menu);
        SearchView searchView = (SearchView) menu.findItem(R.id.menu_action_search).getActionView();
        return true;
    }

For some more reading this tutorial is very very good on using ActionBar:

http://www.vogella.com/tutorials/AndroidActionBar/article.html

zzlalani
  • 22,960
  • 16
  • 44
  • 73
Manolis Proimakis
  • 1,787
  • 16
  • 22
  • Thanks..Is there no need to create and include a different xml layout for action bar...Can it be done by using the java part only.. – divyang7 Jul 26 '13 at 19:39
  • how to place transparent actionbar for all activities i have a multiple screens with fullscreen and also integrate android-flip animation for page curl animation for that i need to place actionbar z-index is top like in ios thing please help for reference News in Shorts- India News App – Harsha Jul 10 '15 at 06:18
  • @ManosProm If I want to use the same "**onOptionsItemSelected**" on all the activities of app. How can I do that or I have to write the same code on each view. – Shuddh Jul 25 '16 at 14:05