Dima
Dima

Reputation: 53

android actionbar for multiple activities

I have application that have multiple screens ( ViewPager, Activities ). My question is how can I display different action bar in each of this screens?

multiple xml files ?

one xml file and add / replace existing one ?

Thanks

Upvotes: 0

Views: 1666

Answers (1)

Alireza Ahmadi
Alireza Ahmadi

Reputation: 5338

If you don't want to change theme and you only need different action item for each activity you can do it simply by inflating different menu item for each activity

create new file under src/menu/activity_one.xml (if menu directory does not exist create one) and put menu items on it. here is an example

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_flip"
        android:title="@string/flip"
        android:showAsAction="always"/>

</menu>

Then in ActivityOne add this code.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activity_one,menu);
    return super.onCreateOptionsMenu(menu);
}

This way you inflated that especific menu items for ActivityOne. If you have another Activity lets say ActivityTwo just inflate different menu file!

And here is how you can define witch action selected by user, just in case!

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_flip:
            flipCard("left");
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }

}

Upvotes: 1

Related Questions