Violet Giraffe
Violet Giraffe

Reputation: 33597

Different menu xmls for different screen sizes

Is it possible to define different menus for different screen sizes? I've tried adding folders res/menu-large and res/menu-xlarge, but the menu xmls I place here have no effect on my xlarge tablet.

Upvotes: 3

Views: 1112

Answers (2)

daneejela
daneejela

Reputation: 14223

You can create different menu folders for different screen sizes, for example:

res/menu res/menu-sw600dp res/menu-sw720dp

etc..

Then put your menu files in corresponding folders and they will be picked up at runtime.

Upvotes: 0

Opiatefuchs
Opiatefuchs

Reputation: 9870

Not a real answer, just an idea, but too long for a comment:

I don´t think that this is supported, it seems that this is not documented. But a possible idea is, to set booleans on different values. For example, you could set som res-values folder:

res/values-xlarge res/values-large res/values-sw600dp

and so on. In that values, You could declare a boolean:

    <resources>
<bool name="isXLarge">true</bool>
    </resources>

or

    <resources>
<bool name="isLarge">true</bool>
    </resources>

etc...

Then create different menu layouts which You could start if one of the values are true.

menu_large.xml menu_xlarge.xml menu_sw620dp.xml

And at createOptionsMenu:

    @Override
  public boolean onCreateOptionsMenu(Menu menu) {
   MenuInflater inflater = getMenuInflater();
   boolean xlargeValue = getResources().getBoolean(R.bool.isXlarge);
   boolean largevalue = getResources().getBoolean(R.bool.isLarge);
   boolean tabletValue = getResources().getBoolean(R.bool.sw620dp):

   if(xlargeValue==true){
   inflater.inflate(R.menu.menu_xlarge, menu);
   }else if(largevalue==true){
    inflater.inflate(R.menu.menu_large, menu);
   }else if(tabletValue==true){
      inflater.inflate(R.menu.menu_sw620dp, menu);
    }

   return true;
  }

I have never tested it, because the generated menu looks good on all screen sizes if you set different drawables to the drawable folders.

Upvotes: 1

Related Questions