I have a navigation drawer. When an event is called, I want to hide one of my navigation menu item for user. How can I do that?
protected void onCreate(Bundle sa
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Menu menu =navigationView.getMenu();
MenuItem target = menu.findItem(R.id.nav_target);
target.setVisible(false);
You can delete the second item of listview via following code:
navDrawerItems.remove(2);
((BaseAdapter)adapter).notifyDataSetChanged();
Hide/Show Navigation item
Make changes in your NavDrawerListAdapter as follow,
public class MyListAdapter extends BaseAdapter {
Context context;
ArrayList<ItemListModel> listMenuItem;
ArrayList<ItemListModel> listMenuItem1;
int hideViewIndex = -1;
public MyListAdapter(Context context, ArrayList<ItemListModel> listMenuItem) {
this.context = context;
this.listMenuItem = listMenuItem;
listMenuItem1=new ArrayList<ItemListModel>(listMenuItem);
}
@Override
public int getCount() {
return listMenuItem.size();
}
@Override
public Object getItem(int position) {
return listMenuItem.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHolder
{
private ImageView imgLogo;
private TextView txtItemName;
}
public void hideView(int pos)
{
listMenuItem.remove(pos);
notifyDataSetChanged();
}
public void showView(int pos)
{
listMenuItem.add(pos, listMenuItem1.get(pos));
notifyDataSetChanged();
}
@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if(convertView == null)
{
LayoutInflater myLayoutInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = myLayoutInflater.inflate(R.layout.activity_item_list, null);
viewHolder = new ViewHolder();
viewHolder.imgLogo = (ImageView) convertView.findViewById(R.id.imgLogo);
viewHolder.txtItemName = (TextView) convertView.findViewById(R.id.txtItemName);
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
ItemListModel itemListModel = listMenuItem.get(position);
viewHolder.imgLogo.setImageDrawable(itemListModel.getImageLogo());
viewHolder.txtItemName.setText(itemListModel.getStrItemName());
return convertView;
}
and call this method in activity as follow,
adapter.hideView(2);
//adapter.showView(2);
This will work as setInvisibility(GONE):
Menu nav_Menu = navigationView.getMenu();
nav_Menu.findItem(R.id.nav_profile).setVisible(false);
nav_Menu.findItem(R.id.menu_login).setVisible(true);
nav_Menu.findItem(R.id.menu_logout).setVisible(false);
nav_Menu.findItem(R.id.menu_regsitration).setVisible(true);
NavigationView navigationView;
mapIntentActivity = getIntent();
navItemString = mapIntentActivity.getStringExtra("success");
if(navItemString != null) {
navigationView = (NavigationView) findViewById(R.id.nav_view);
Menu nav_Menu = navigationView.getMenu();
nav_Menu.findItem(R.id.nav_car).setVisible(false);
}
Kotlin version
val navigationView: NavigationView = findViewById(R.id.nav_view) as NavigationView
val nav_Menu: Menu = navigationView.getMenu()
nav_Menu.findItem(R.id.nav_stock).setVisible(false)