When we define a NavigationView with a section with sub menu items. It left aligns the sub items with the section title:
<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>
I tried adding a transparent image with the correct size to 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>
But by default the NavigationView:
I could not find how to configure this padding nor the icon size.
Question How can we change the sub item indentation so that the sub items are more indented?
I prefer to do it cleaning via an attribute rather than inserting transparent images.
Disclaimer
See my comment to Noundla on the main question where I explain why I think that indentation is not the right approach.
Answer
With that said, if you must have indentation the simplest way is to pad each menu item with spaces. This is not ideal but it is simple to implement, understand and replace when a better option is available. And it works with the built-in Android NavigationView without having to introduce external libraries:
Here is the code sample that will work:
<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>
I hope this saves someone out there time.
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 the SecondaryDrawerItem and add the padding on 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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With