Here is the screen shot of my sidebar navigation bar.
I want to make the items in the sidebar navigation Menu clickable so that I can move to another Activity how can I do this?
You have to treat these as Menu items.
When you set up the navigation bar from the layout XML you attach a menu.xml to it which contains menu items that are then displayed in the navigation bar.
Method 1:
Now from the activity where you have your navigation bar variable you need to get the menu from the navigation view object.
Menu menu = navigation_view.getMenu();
Then you can get individual menuItems from this menu object.
MenuItem myAccount = menu.getItem(0);
MenuItem settingsItem = menu.getItem(1);
MenuItem logoutItem = menu.getItem(2);
Now you can use each of this menuItem objects to set menu click listeners on them and then start the next activity from there. e.g.
myAccount.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
Intent intent = new Intent(context, ToAcitivity.class);
startActivity(intent);
return true;
}
});
Similarly, you can use rest of the objects and make them clickable.
Method 2:
You can also use
navigation_view.setNavigationItemSelectedListener(new
NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case 1:
break;
case 2:
}
return false;
}
});
Second one being more simpler to use
Related
I want to add Bottom navigation and Side navigation drawer in one activity like LinkedIn. I tried to add bottom navigation to the navigation drawer activity but unsuccessful as navigationListener of both can have same names and in one listener how can i separate bottom navigation items and side navigation items.
it doesn't matter if the two listener have the same name, you can add add listener to each one by their full package name.for example you can add listener to Navigation view like this:
navigationView.setNavigationItemSelectedListener(new android.support.design.widget.NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
///your code here
return false;
}
})
and add listener to Bottom navigation view like this :
bottomNavigationView.setOnNavigationItemSelectedListener(new android.support.design.widget.BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
return false;
}
});
I have Activity with Action Which have a refresh icon in OnCreateOption Menu.I want to reload my current Activity On click this Icon Click.But I don't know to use here click event.How to resolve this.
Override onOptionsItemSelected method like this way:
#Override
public boolean onOptionsItemSelected(final MenuItem item) {
Intent intent = new Intent();
switch (item.getItemId()) {
case R.id.mnuRefresh: // your menu item id
reCreate();
break;
}
return false;
}
There is a good example here :
https://stackoverflow.com/a/7480103/2724418
I've added items to NavigationView programmatically:
**HERE ADD ITEMS**
Menu rightMenu = mRightDrawerView.getMenu();
for (DataParking dataParking : dataParkingList) {
MenuItem menuItem = rightMenu.add(dataParking.getTimeParking());
}
**HERE ADD CLICK LISTENER**
mRightDrawerView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// Select menu
menuItem.setChecked(true);
// Closing left_drawer on item click
mDrawerLayout.closeDrawer(mRightDrawerView);
return false;
}
});
Items in navigation drawer are correctly clickable, but the selection is not persistent. If I add the same items via XML all works well.
When you are adding an item programmatically, the item's checkable flag is not set. You should just add this line:
menuItem.setCheckable(true);
after adding an item to menu.
As #Oncky answered, you can just setCheckable on your menuItem like this:
Menu rightMenu = mRightDrawerView.getMenu();
for (DataParking dataParking : dataParkingList) {
MenuItem menuItem = rightMenu.add(dataParking.getTimeParking()).setCheckable(true);
}
I can get all the items from the actionbar with getItem() but I need to compare their id's with a particular id. How can I do this?
You can get actionBar menu items id by calling pMenu.getItem();
You can use changing methods of these menu items by overriding the method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action buttons
switch(item.getItemId()) {
case R.id.action_stats:
break;
}
}
I've been trying to get my actionbar buttons to show on click but can't get it to work. I got 2 buttons and if i click on one i want the other to show and the other to get invinsible.
Here is my code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
MenuItem brandsMenu = (MenuItem)findViewById(R.id.action_brands);
MenuItem categoryMenu = (MenuItem)findViewById(R.id.action_category);
switch (item.getItemId()) {
case R.id.action_category:
brandsMenu.setVisible(true);
return true;
case R.id.action_brands:
categoryMenu.setVisible(true);
}
This only shows errors. Any Suggestions?
You need to call InvalidateOptionsMenu when you want to make changes to your menu.
You then use the onCreateOptionsMenu override to apply those changes.
Define MenuItems named brandsMenu and categoryMenu and initialize them onPrepareOptionsMenu
categoryMenu = menu.findItem(R.id.action_category);
brandsMenu = menu.findItem(R.id.action_brands);
You should be able to change visibility such as categoryMenu.setVisible(true);
You can't set visibility on menuitems.
You should invalidate the options menu and add only the menuItems you want to be visible