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?