Paul Verest
Paul Verest

Reputation: 63972

Android: add actions from Menu to own title bar RelativeLayout

In Android project most Activities have ActionBar disabled with style

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
</style>

and use own title bar that is based on RelativeLayout via include

<include
    android:id="@+id/activity_title_bar"
    layout="@layout/view_title_bar" />

There is no intetion of using ActionBar but I would like to find how to implement adding Menu item on title bar like ActionBar does.

Currently those actions are defined in res/menun/common_actions.xml and visible via options menu.

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_home:
            MainActivity.start(this);
            return true;
        case R.id.action_search:
            SearchActivity.start(this);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}   

However options menu button is not available on newer devices.

How to implement adding actions to title bar RelativeLayout? There is no need for all Actionbar logic, just take 2 actions with icons from defined menu and put them on title bar.

Related docs:

Upvotes: 0

Views: 679

Answers (1)

gatlingxyz
gatlingxyz

Reputation: 753

You should really just use the Toolbar. It's rather quite simple and you can achieve exactly what you want...

..but to achieve what you want, you would use a PopupMenu and anchor it to an ImageView or ImageButton that looks like the overflow menu.

Upvotes: 1

Related Questions