0

I am developing an Android App. In my app, I am using navigation drawer and navigation view. But I set the menu item for them programmatically. For a menu item, I set both icon and title. But I am having a problem with that. That is I cannot set the spacing between icon and title that I programmatically added to menu.

This is how I programmatically add item to menu

Menu menu = leftDrawer.getMenu();
        SubMenu subMenu = menu.addSubMenu(MAIN_MENU_ITEM_GROUP_ID, 99, 99, "Others");

        subMenu.add(MAIN_MENU_ITEM_GROUP_ID,96,96,"Monthly Leaderboard").setIcon(R.drawable.leaderboard_icon).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                startActivity(new Intent(MainActivity.this, LeaderboardActivity.class));
                return false;
            }
        });

        subMenu.add(MAIN_MENU_ITEM_GROUP_ID,96,96,"Settings").setIcon(R.drawable.settings_icon).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                startActivity(new Intent(MainActivity.this, SettingsActivity.class));
                return false;
            }
        });

But spacing between between icon and title is not properly set.

This is the screenshot of my problem

enter image description here

As you can see in screenshot, spacing between left side of screen and icon and spacing between icon and title are not same. How can I set the spacing between icon and title of menu item programmatically?

halfer
  • 19,824
  • 17
  • 99
  • 186
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372

1 Answers1

3

They're not supposed to be the same. From the Navigation Drawer guidelines, you'll note this screenshot:

Navigation Drawer specs

The side margin is 16dp (and 24dp on tablets) while the titles are aligned with the 72dp keyline.

Since the NavigationView seeks to exactly match the material design guidelines, there is no way to change the spacing.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443