Create Android MenuItem with subtitles programmatically

I need to have DrawerMenu with programmatically generated elements, I can add menuItems, but only with a title and icon, I want them to also have subtitles (as in the picture)

enter image description here

I get Drawer by its identifier, then by its menu and then add MenuItems as follows (code snippet from my MainActivity):

int id =0;
String MenuTitle = "Title of the item in menu";
NavigationView mDrawerList = (NavigationView)findViewByID(R.id.nav_view);
Menu menu = mDrawerList.getMenu();
MenuItem menuItem;
menuItem = menu.add (Menu.NONE, id, Menu.NONE, title);
menuItem.setIcon(R.drawable.ic_action_home);
menuItem.setCheckable(true);
+6
source share
4 answers

It currently does not allow subTitle menus to be added to it. NavigationView

But if you want to achieve this, I would recommend that you take the ListViewnested inside NavigationView. . So that you can customize your ListView cell, you need to.

<android.support.design.widget.NavigationView
    android:id="@+id/navigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include layout="@layout/nav_header" />

        <ListView
            android:id="@+id/lst_menu_items"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>
</android.support.design.widget.NavigationView>

. app: NavigationView, ListView . ListView .

+5

onPrepareOptionsMenu() .

 @Override
public boolean onPrepareOptionsMenu(Menu menu) {
    menu.clear();

    menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon);

    return super.onPrepareOptionsMenu(menu);
}
0

, SpannableString

String MenuTitle = "Title of the item in menu";

SpannableString spannablecontent=new SpannableString(MenuTitle);
spannablecontent.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 
                         0,spannablecontent.length(), 0);

,

menuItem = menu.add (Menu.NONE, id, Menu.NONE, spannablecontent);

Concat two SpannableString

SpannableString: RelativeSizeSpans?

0

:

private Menu menu;

// ...

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
  this.menu = menu;
  getMenuInflater().inflate(R.menu.options, menu);
  return true;
}

// ...

private void hideOption(int id)
{
  MenuItem item = menu.findItem(id);
  item.setVisible(false);
}

private void showOption(int id)
{
  MenuItem item = menu.findItem(id);
  item.setVisible(true);
}

private void setOptionTitle(int id, String title)
{
  MenuItem item = menu.findItem(id);
  item.setTitle(title);
}

private void setOptionIcon(int id, int iconRes)
{
  MenuItem item = menu.findItem(id);
  item.setIcon(iconRes);
}
-1

Source: https://habr.com/ru/post/1630065/


All Articles