How to make menuItem invisible with no id?
How to make menuItem invisible with no id?
Is it possible to make a menuItem invisible with no id. I have a navigation drawer and it works but don't have a XML file.
Usually you do like this
MenuItem item = findViewById()
MenuItem item = findViewById()
item.setVisible(false);
item.setVisible(false);
My code
private NavigationItem createNavigation(String names){
Log.d(TAG, "createNavigation");
navItems = new NavigationItem[names.length];
for(int i = 0; i < names.length; i++){
Navigation navTo = Navigation.MyPage;
if(names[i].compareTo(getResources().getStringArray(R.array.navigation)[0]) == 0){
navTo = Navigation.MyPage;
if(currentFragment instanceof MyPageFragment)
navItems[i] = new NavigationItem(names[i], R.drawable.ic_mypage_norm, navTo);
else
navItems[i] = new NavigationItem(names[i], R.drawable.ic_mypage_norm, navTo);
}
else if(names[i].compareTo(getResources().getStringArray(R.array.navigation)[1]) == 0){
navTo = Navigation.Book;
if(currentFragment instanceof BookFragment)
navItems[i] = new NavigationItem(names[i], R.drawable.ic_book_norm, navTo);
else
navItems[i] = new NavigationItem(names[i], R.drawable.ic_book_norm, navTo);
}
else if(names[i].compareTo(getResources().getStringArray(R.array.navigation)[2]) == 0){
navTo = Navigation.Status;
if(currentFragment instanceof StatusFragment)
navItems[i] = new NavigationItem(names[i], R.drawable.ic_status_norm, navTo);
else
navItems[i] = new NavigationItem(names[i], R.drawable.ic_status_norm, navTo);
}
else if(names[i].compareTo(getResources().getStringArray(R.array.navigation)[3]) == 0){
navTo = Navigation.Information;
if(currentFragment instanceof InfoFragment)
navItems[i] = new NavigationItem(names[i], R.drawable.ic_info_norm, navTo);
else
navItems[i] = new NavigationItem(names[i], R.drawable.ic_info_norm, navTo);
}
else if(names[i].compareTo(getResources().getStringArray(R.array.navigation)[4]) == 0){
navTo = Navigation.Settings;
if(currentFragment instanceof SettingsFragment)
navItems[i] = new NavigationItem(names[i], R.drawable.ic_setting_norm, navTo);
else
navItems[i] = new NavigationItem(names[i], R.drawable.ic_setting_norm, navTo);
}
else if(names[i].compareTo(getResources().getStringArray(R.array.navigation)[5]) == 0){
navTo = Navigation.PanoramaHome;
if(currentFragment instanceof PanoramaHomeFragment){
navItems[i] = new NavigationItem(names[i], R.drawable.ic_setting_norm, navTo);
Log.d(TAG, "createNavigation: if");
}
else
navItems[i] = new NavigationItem(names[i], R.drawable.ic_setting_norm, navTo);
Log.d(TAG, "createNavigation: else");
}
//navItems[i] = new NavigationItem(names[i], R.drawable.pholder_icon, navTo);
}
return navItems;
}
void navigateTo(NavigationItem ni){
Log.d(TAG, "navigateTo");
Fragment newFragment = null;
String fragTag = null;
if(ni.isMyPage()){
fragTag = getString(R.string.mypage);
newFragment = fragMan.findFragmentByTag(fragTag);
if(newFragment == null)
newFragment = new MyPageFragment();
}
else if(ni.isBooking()){
fragTag = getString(R.string.booking);
newFragment = fragMan.findFragmentByTag(fragTag);
if(newFragment == null)
newFragment = new BookFragment();
}
else if(ni.isStatus()){
fragTag = getString(R.string.status);
newFragment = fragMan.findFragmentByTag(fragTag);
if(newFragment == null)
newFragment = new StatusFragment();
}
else if(ni.isInfo()){
fragTag = getString(R.string.info);
newFragment = fragMan.findFragmentByTag(fragTag);
if(newFragment == null)
newFragment = new InfoFragment();
}
else if(ni.isSettings()){
fragTag = getString(R.string.settings);
newFragment = fragMan.findFragmentByTag(fragTag);
if(newFragment == null)
newFragment = new SettingsFragment();
}
//TODO CHANGE TO PANORAMA HOME Fragment.
else if(ni.isPanoramaHome()){
fragTag = getString(R.string.panorama_home);
newFragment = fragMan.findFragmentByTag(fragTag);
if(newFragment == null)
newFragment = new PanoramaHomeFragment();
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
currentFragment = newFragment;
transaction.replace(R.id.main_fragment, newFragment, fragTag);
transaction.addToBackStack(null);
transaction.commit();
drawerLayout.closeDrawer(drawerList);
supportInvalidateOptionsMenu();
navDrawerSetup();
setLoading(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.d(TAG, "onCreateOptionsMenu");
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
if(currentFragment != null){
if(currentFragment instanceof BookFragment){
CacheController ch = new CacheController(this);
ch.openDatabases();
if(ch.getAvailableLocalPreChoices() != 1)
inflater.inflate(R.menu.bookmenu, menu);
ch.closeDatabases();
}
else if(currentFragment instanceof StatusFragment || currentFragment instanceof InfoFragment || currentFragment instanceof MyPageFragment){
inflater.inflate(R.menu.refreshmenu, menu);
}
else{
inflater.inflate(R.menu.nomenu, menu);
}
//need id for the fragment to work.
//MenuItem item = findViewById()
//item.setVisible(false);
MenuItem m = menu.getItem(1);
m.setVisible(false);
}
return super.onCreateOptionsMenu(menu);
}
1 Answer
1
You can change the visibility of a menu item using the position:-
MenuItem m = menu.getItem(2);
m.setVisible(false);
I hope this will work for you @silversoul
Hi! I did try it. However I always get a java.lang.IndexOutOfBoundsException. Still thank you for answering :)
– silversoul
Jun 29 at 9:12
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Show full code.
– Cagri Yalcin
Jun 29 at 8:51