How to change indentation of submenu items in NavigationView?

When we define a NavigationView with a section with submenu items. It aligns the subparagraphs with the section heading on the left:

<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:title="Sub items"> <menu> <item android:title="Sub item 1" /> <item android:title="Sub item 2" /> </menu> </item> </menu> 

enter image description here

I tried adding a transparent image with the correct size for the pad:

 <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:title="Sub items"> <menu> <item android:icon="@drawable/ic_transparent" android:title="Sub item 1" /> <item android:icon="@drawable/ic_transparent" android:title="Sub item 2" /> </menu> </item> </menu> 

enter image description here

But by default NavigationView:

  • Adds a fixed pad between the text icon
  • Provides the size of the fixes to the icon itself

I could not find how to configure this add-on or icon size.

Question How can we change the indentation of a subheading so that the subparagraphs are more indented?

I prefer to do the cleanup using the attribute rather than pasting transparent images.

+7
android material-design menu submenu
source share
2 answers

Renouncement

See my comment on Noundla for a basic question, where I explain why I think indentation is the wrong approach.

Answer

With that said, if you should be indented, the easiest way is to fill in each menu item with spaces. This is not ideal, but easy to implement, understand and replace when the best option is available. And it works with the built-in Android NavigationView without the need to contribute external libraries:

Here is an example of code that will work:

 <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:title="Sub items"> <menu> <item android:title="&#160;&#160;&#160;&#160;Sub item 1" /> <item android:title="&#160;&#160;&#160;&#160;Sub item 2" /> </menu> </item> </menu> 

I hope this saves someone there.

+8
source share

Not sure if you got the answer, but I had the same problem and ended up using MaterialDrawer - https://github.com/mikepenz/MaterialDrawer .

You need to extend SecondaryDrawerItem and add padding to bindView onPostBindView.

 drawer = new DrawerBuilder() .withActivity(this) .withHeader(drawerHeader) .withSavedInstance(savedInstanceState) .addDrawerItems( new PrimaryDrawerItem().withName("Item1"), new CustomSecondaryDrawerItem().withName("SubItem1"), new CustomSecondaryDrawerItem().withName("SubItem2") ) .build(); 

CustomDrawerSecondaryItem.java

 public class CustomSecondaryDrawerItem extends SecondaryDrawerItem { @Override public void onPostBindView(IDrawerItem drawerItem, View view) { Context ctx = view.getContext(); int paddingLeft = ctx.getResources().getDimensionPixelSize(R.dimen.drawer_secondary_item_padding_left); view.setPadding(paddingLeft, view.getPaddingTop(), view.getPaddingRight(), view.getPaddingBottom()); super.onPostBindView(drawerItem, view); } } 
+2
source share

All Articles