0

I have a navigationview with 2 groups, in one of the groups I add a new item programmatically using this example: How to add an item to a menu group in NavigationView

How to create and get the id for the newly added item and set OnClickListener on him? (because I want to send some to to a database and get).

I want to create this behaviour: the behaviour I want to create

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">

<group
    android:id="@+id/group1"
    android:checkableBehavior="single">
    <item
        android:id="@+id/nav_today"
        android:icon="@drawable/ic_today"
        android:orderInCategory="0"
        android:title="@string/today" />
    <item
        android:id="@+id/nav_inbox"
        android:icon="@drawable/ic_inbox"
        android:orderInCategory="0"
        android:title="@string/inbox_text" />
</group>


<group
    android:id="@+id/group2"
    android:checkableBehavior="none">

    <item
        android:id="@+id/nav_add_list"
        android:icon="@drawable/ic_add"
        android:orderInCategory="1"
        android:title="@string/add_list_text" />
    <item
        android:id="@+id/nav_random_task"
        android:icon="@drawable/ic_random_task"
        android:orderInCategory="1"
        android:title="@string/random_task" />
    <item
        android:id="@+id/nav_account"
        android:icon="@drawable/ic_account"
        android:orderInCategory="1"
        android:title="@string/account" />
</group>

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(R.style.BlueTheme);
    setContentView(R.layout.activity_home_drawer);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TaskBottomSheet taskBottomSheet = new TaskBottomSheet();
            taskBottomSheet.showNow(getSupportFragmentManager(), "taskBottomSheet");
        }
    });
    drawer = findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    navigationView = findViewById(R.id.nav_view);
    if (!TextUtils.isEmpty(listName)) {
        addList(listName);
    }



    private void addList(String listName) {
    Menu menu = navigationView.getMenu();
    menu.add(R.id.group1, Menu.NONE, 0, listName).setIcon(R.drawable.ic_list);
}

I tried like this:

navigationView = findViewById(R.id.nav_view);
    Menu menu = navigationView.getMenu();
    final MenuItem menuItem = menu.add(R.id.group1, Menu.NONE, 0,"Read").setIcon(R.drawable.ic_list);

    menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Toast.makeText(HomeDrawerActivity.this, "Item clicked", Toast.LENGTH_SHORT).show();
            menuItem.setCheckable(true);
            if (drawer.isDrawerOpen(GravityCompat.START)){
                drawer.closeDrawer(GravityCompat.START);
            }
            return true;
        }
    });

Another problem is the addList method. The newly menu added is not showed.Only when I add the menu directly after navigationView = findViewById the the item is showing.How to add multiple items and setOnClickListeners for them?

Scepticul
  • 45
  • 6
  • add your code, no one can help you without seeing what you are doing in code. – Haider Saleem Mar 09 '20 at 20:30
  • There's no need to set individual `OnMenuItemClickListener`s for each new `MenuItem`. If you pass a unique ID for each in the `add()` call – instead of `Menu.NONE` – you can handle them all with a single `OnNavigationItemSelectedListener` set on the `NavigationView`. [Here's a basic example of such a setup](https://drive.google.com/file/d/11M6pW1S7jR63bsO6OBu_n1pQqwIdZ0du/view?usp=drivesdk) that I'd put together to demonstrate this for a previous, similar question. – Mike M. Mar 09 '20 at 22:20
  • Thank you! Let me see how it works. – Scepticul Mar 09 '20 at 23:53
  • "... the items will be created when user adds a new item to the group." – That doesn't mean that you can't provide a unique ID for each, though I see now that my previous comment is somewhat misleading. You don't _have_ to use IDs to distinguish items; that's just how I'd done it. You could instead simply use `item.getTitle()` to distinguish the `MenuItem`s in `onNavigationItemSelected()`. – Mike M. Mar 09 '20 at 23:55
  • The problem here is that I don't know how to user is going to name his items, for example: let's say that thar user is adding an item called books(in witch he will add a list of his books), how do I you check if he is clicking this item –to run a query and show his list of books – instead of another if I don't know what items is he adding? – Scepticul Mar 10 '20 at 00:06
  • 1
    I solved the problem, thank you for help! :) – Scepticul Mar 10 '20 at 00:12

0 Answers0