1

I am working in an app with this kind of structure:

The app has no initial ActionBar so I add it on the OnCreate on each Activity this way:

  android.support.v7.widget.Toolbar barra =(android.support.v7.widget.Toolbar) getLayoutInflater().inflate(local.quick_stuff.R.layout.barra_herramientas,null);
    LinearLayout layout_actividad = (LinearLayout)findViewById(R.id.cp_lista_receta);
    layout_actividad.addView(barra,0);
    setSupportActionBar(barra);
    getSupportActionBar().setTitle("Listado de Recetas");

Then I try to inflate a menu.xml to the ActionBar so the code changes to this:

    android.support.v7.widget.Toolbar barra =(android.support.v7.widget.Toolbar) getLayoutInflater().inflate(local.quick_stuff.R.layout.barra_herramientas,null);
    barra.inflateMenu(R.menu.menu_barra);
    LinearLayout layout_actividad = (LinearLayout) findViewById(R.id.cp_lista_receta);
    layout_actividad.addView(barra,0);
    setSupportActionBar(barra);
    getSupportActionBar().setTitle("Listado de Recetas");         
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

But the result is a blank ActionBar

The Menu.xml file

  <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/filtro"
        android:icon="@drawable/filtrado_icono"
        android:title="Filtrado"
        android:visible="true"
        app:showAsAction="ifRoom"/>
</menu>

At the end of the day i am trying to do this to have flexibility because a want to have different menu.xml to inflate at my ActionBar. I am thinking this could be kind of flexible but has too many branching coding.

Any help?

PRATEEK BHARDWAJ
  • 2,364
  • 2
  • 21
  • 35
Albert
  • 58
  • 7
  • Related: https://stackoverflow.com/questions/26511981/toolbar-inflatemenu-seems-to-do-nothing – lucidbrot Jan 07 '18 at 16:14
  • Also Related: [The docs](https://developer.android.com/guide/topics/ui/menus.html#options-menu) – lucidbrot Jan 07 '18 at 16:17
  • Actually I found out the answers with the comments. A soon you set the custom toolbar as Supportactionbar. The activity takes control of the menu options defined on onCreateOptions. So its just override the method in the activity. – Albert Jan 08 '18 at 17:42
  • You could write that as an answer and accept it then – lucidbrot Jan 08 '18 at 17:47
  • @Albert Don't get me wrong, but it is very basic question, that was asked on stack 5 years ago and has many answers. Also you can easily google it. + it is well described on google android documentation page, with examples. Why ask and have so many duplicate questions on so ? – Misha Akopov Jan 11 '18 at 07:04

1 Answers1

0

Just for sake of closing, and help if someone needs it: if you call setSupportActionBar(barra);

You must override onCreateOptionsMenu() because the activity takes control of the toolbar and the Menu must be defined by that method. So I just added this to the activity code.

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_barra, menu);
    return true;
}
Albert
  • 58
  • 7