I have 3 menus in my navigation view where each menu have few more items.
Im having problems when i press each item in menu, i want checked item to be selected but other items non selected.
Bellow is picture so that you can see what is the problem:
For selection im using item.setChecked(true); in onNavigationItemSelected(MenuItem menu).
What i want is only 1 item to be selected, so when i checked one item, i want that other items are not selected.
I search on google but usually they say that i need to separate each menu into group android:checkableBehavior="single" what i done but, it still do not work:
EDIT START:
I just find solution, instead of item.setChecked(true) i write navigationView.setCheckedItem(R.id.itemId); and everything works now.
I find solution on this link:
EDIT END
Here is the code for drawer_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Animals">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/dog"
android:icon="#drawable/ic_action_dog"
android:title="Domestic Animals" />
<item
android:id="#+id/wild"
android:icon="#drawable/ic_action_dog"
android:title="Wild Animals" />
<item
android:id="#+id/bird"
android:icon="#drawable/ic_action_bird"
android:title="Birds" />
<item
android:id="#+id/fish"
android:icon="#drawable/ic_action_fish"
android:title="Sea Animals" />
<item
android:id="#+id/insects"
android:icon="#drawable/ic_action_bubamara"
android:title="Insects" />
</group>
</menu>
</item>
<item android:title="Others">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/car"
android:icon="#drawable/ic_action_car"
android:title="Cars" />
<item
android:id="#+id/smiley"
android:icon="#drawable/ic_action_smiley"
android:title="Laugh" />
<item
android:id="#+id/earth"
android:icon="#drawable/ic_action_earth"
android:title="Nature" />
<item
android:id="#+id/sounds"
android:icon="#drawable/ic_action_zvucnik"
android:title="Effects" />
<item
android:id="#+id/tools"
android:icon="#drawable/ic_action_clock"
android:title="Others" />
</group>
</menu>
</item>
<item android:title="Change pictures">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/change_picture_id"
android:icon="#drawable/ic_action_promjena_slike"
android:title="Change Picture"></item>
</group>
</menu>
</item>
</menu>
Here is the code for navigation drawer menu item click events in main activity:
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.dog:
homeFragment.sortiranje("Domestic", tagonja);
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.wild:
homeFragment.sortiranje("Wild", tagonja);
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.bird:
homeFragment.sortiranje("Birds", tagonja);
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.fish:
homeFragment.sortiranje("Sea", tagonja);
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.insects:
homeFragment.sortiranje("Insects", tagonja);
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.car:
homeFragment.sortiranje("Cars", tagonja);
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.smiley:
homeFragment.sortiranje("Laugh", tagonja);
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.earth:
homeFragment.sortiranje("Nature", tagonja);
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.sounds:
homeFragment.sortiranje("Effects", tagonja);
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.tools:
homeFragment.sortiranje("Others", tagonja);
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.change_picture_id:
item.setChecked(true);
if (tagonja == 0) {
tagonja = 1;
homeFragment.promjenaSlike(tagonja);
} else if (tagonja == 1) {
tagonja = 0;
homeFragment.promjenaSlike(tagonja);
}
drawerLayout.closeDrawers();
break;
}
return false;
}
});
Related
I cant click on item inside my menu. My menu opens and everything is displayed normally, but nothing happens when I click. I don't know why and searched for information but it doesn't help me.
This my XML code profile__detail_menu.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
app:showAsAction="always"
android:icon="#drawable/ic_baseline_more_vert_24"
android:title="">
<menu>
<item
android:id="#+id/change_name"
android:title="Change name"
android:icon="#drawable/ic_baseline_edit_24"
/>
<item
android:id="#+id/new_photo"
android:title="New photo"
android:icon="#drawable/ic_baseline_add_a_photo_24"
/>
<item
android:id="#+id/log_out"
android:title="Log out"
android:icon="#drawable/ic_baseline_login_purp_24"
/>
</menu>
</item>
</menu>
This my Java code.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.profile__detail_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
Toast.makeText(Profile.this,"Something", Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.change_name:
Toast.makeText(this, "change_name selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.new_photo:
Toast.makeText(this, "new_photo selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.log_out:
Toast.makeText(this, "log_out selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The thing which I feel is wrong is your menu XML file. For some reason you have a menu tag inside a menu tag. So there is sort of a sub menu if you get my point? But you're not inflating that actually.
Modify the XML as follows
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".YourActivityName"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/change_name"
android:title="Change name"
app:showAsAction="always|withText"
android:icon="#drawable/ic_baseline_edit_24"
/>
<item
android:id="#+id/new_photo"
android:title="New photo"
app:showAsAction="always|withText"
android:icon="#drawable/ic_baseline_add_a_photo_24"
/>
<item
android:id="#+id/log_out"
android:title="Log out"
app:showAsAction="always|withText"
android:icon="#drawable/ic_baseline_login_purp_24"
/>
</menu>
I have updated your menu as per you code. If you need, add more items to it.
by default, menu titles and icons display in bottom navigation view.
how can we hide/show titles/icons of menu items programmically or in XML?
note: hide/show one of these: 'titles' or 'icons' (not both)
menu :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/home"
android:icon="#drawable/home"
android:title="#string/home"/>
<item
android:id="#+id/about"
android:icon="#drawable/about" />
<item
android:id="#+id/services"
android:icon="#drawable/services" />
<item
android:id="#+id/portfolios"
android:icon="#drawable/portfolios" />
<item
android:id="#+id/contact"
android:icon="#drawable/contact" />
</menu>
method:
private void bnvHelper(){
BottomNavigationView bnv = (BottomNavigationView)findViewById(R.id.bottom_navigation_view);
bnv.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int[] titles = {
R.string.home,
R.string.about,
R.string.services,
R.string.portfolios,
R.string.contact};
BottomNavigationView bnv = (BottomNavigationView)findViewById(R.id.bottom_navigation_view);
Menu menu = bnv.getMenu();
final int previousItem = bnv.getSelectedItemId();
final int nextItem = item.getItemId();
if (previousItem != nextItem) {
switch (nextItem) {
case R.id.home:
menu.getItem(0).setTitle(titles[0]);
menu.getItem(1).setTitle(null);
menu.getItem(2).setTitle(null);
menu.getItem(3).setTitle(null);
menu.getItem(4).setTitle(null);
break;
case R.id.about:
menu.getItem(1).setTitle(titles[1]);
menu.getItem(0).setTitle(null);
menu.getItem(2).setTitle(null);
menu.getItem(3).setTitle(null);
menu.getItem(4).setTitle(null);
break;
case R.id.services:
menu.getItem(2).setTitle(titles[2]);
menu.getItem(0).setTitle(null);
menu.getItem(1).setTitle(null);
menu.getItem(3).setTitle(null);
menu.getItem(4).setTitle(null);
break;
case R.id.portfolios:
menu.getItem(3).setTitle(titles[3]);
menu.getItem(0).setTitle(null);
menu.getItem(1).setTitle(null);
menu.getItem(2).setTitle(null);
menu.getItem(4).setTitle(null);
break;
case R.id.contact:
menu.getItem(4).setTitle(titles[4]);
menu.getItem(0).setTitle(null);
menu.getItem(1).setTitle(null);
menu.getItem(2).setTitle(null);
menu.getItem(3).setTitle(null);
break;
}
} return true;
}
});
}
The internet is filled with answers to a different question (How to sort items in a group? The answer is to use OrderInCategory. My question is how to sort or order the groups themselves?
In my app, I have to programmatically add/remove menu items in each group as well as show/hide entire groups at a time.
I have things setup something like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:id="#+id/nav_menu_stuff1">
</group>
<group android:id="#+id/nav_menu_stuff2">
</group>
<group android:id="#+id/nav_menu_stuff3">
</group>
<group android:id="#+id/nav_menu_stuff4">
</group>
</menu>
And then in code, I add/remove:
getMenu().add(R.id.nav_menu_stuff1, some_id, 0, some_name);
getMenu().removeItem(some_id);
as well as show/hide entire groups at a time:
getMenu().setGroupVisible(R.id.nav_menu_stuff1, stuff1_is_visible);
getMenu().setGroupVisible(R.id.nav_menu_stuff2, stuff2_is_visible);
getMenu().setGroupVisible(R.id.nav_menu_stuff3, stuff3_is_visible);
getMenu().setGroupVisible(R.id.nav_menu_stuff4, stuff4_is_visible);
The problem for me is that Android doesn't maintain the order of the groups. At some point I clear all the items from a group in the middle (e.g stuff2) and then add items back in. When I do that, the group ends up at the bottom of the NavigationView. That's not where I want it.
How do we maintain the order of the groups?
Try this
use visible true/false rather than add/remove
navigationView = (NavigationView) findViewById(R.id.nav_view);
Menu nav_Menu = navigationView.getMenu();
nav_Menu.findItem(R.id.nav_settings).setVisible(false);
try this code:
menu bar xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home_black_24dp"
android:title="#string/nav_home" />
<item
android:id="#+id/nav_photos"
android:icon="#drawable/ic_photo_library_black_24dp"
android:title="#string/nav_photos" />
<item
android:id="#+id/nav_movies"
android:icon="#drawable/ic_local_movies_black_24dp"
android:title="#string/nav_movies" />
<item
android:id="#+id/nav_notifications"
android:icon="#drawable/ic_notifications_black_24dp"
android:title="#string/nav_notifications" />
<item
android:id="#+id/nav_settings"
android:icon="#drawable/ic_settings_black_24dp"
android:title="#string/nav_settings" />
</group>
<item android:title="Other">
<menu>
<item
android:id="#+id/nav_about_us"
android:title="#string/nav_about_us" />
<item
android:id="#+id/nav_privacy_policy"
android:title="#string/privacy_policy" />
</menu>
</item>
like this:
java class use menu bar
private void selectNavMenu() {
navigationView.getMenu().getItem(navItemIndex).setChecked(true);
}
private void setUpNavigationView() {
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()) {
//Replacing the main content with ContentFragment Which is our Inbox View;
case R.id.nav_home:
navItemIndex = 0;
CURRENT_TAG = TAG_HOME;
break;
case R.id.nav_photos:
navItemIndex = 1;
CURRENT_TAG = TAG_PHOTOS;
break;
case R.id.nav_movies:
navItemIndex = 2;
CURRENT_TAG = TAG_MOVIES;
break;
case R.id.nav_notifications:
navItemIndex = 3;
CURRENT_TAG = TAG_NOTIFICATIONS;
break;
case R.id.nav_settings:
navItemIndex = 4;
CURRENT_TAG = TAG_SETTINGS;
break;
case R.id.nav_about_us:
// launch new intent instead of loading fragment
startActivity(new Intent(MainActivity.this, AboutUsActivity.class));
drawer.closeDrawers();
return true;
case R.id.nav_privacy_policy:
// launch new intent instead of loading fragment
startActivity(new Intent(MainActivity.this, PrivacyPolicyActivity.class));
drawer.closeDrawers();
return true;
default:
navItemIndex = 0;
}
//Checking if the item is in checked state or not, if not make it in checked state
if (menuItem.isChecked()) {
menuItem.setChecked(false);
} else {
menuItem.setChecked(true);
}
menuItem.setChecked(true);
loadHomeFragment();
return true;
}
});
it helps you
I have create a menu in the action bar but I don't know how to use the items inside of it as a button.
That's my menu 'xml' code:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:icon="#drawable/ic_add_circle_outline_black_24dp"
android:title=""
app:showAsAction="ifRoom">
<menu>
<item
android:id="#+id/deleteMenu"
android:title="Delete All" />
<item
android:id="#+id/exitMenu"
android:title="Exit" />
</menu>
</item>
<item
android:id="#+id/addMovieOffline"
android:title="Offline Mode" />
<item
android:id="#+id/addMovieOnline"
android:title="Online Mode" />
</menu>
That's what i have in java:
public class MyMainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_main);
}
// "Creating" my menu.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
}
How I use the items I just declare?
For example the "Exit" option
I think it is the most basic thing although I don't know how to reach the item as a button.. Or its already define it self as a button ?
I would like to get an example and explanation.
you are using menu item inside the item, which is not the correct way to group item in menu.
Try this xml to generate your menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:id="#+id/my_menu" android:checkableBehavior="single">
<item
android:id="#+id/deleteMenu"
android:title="Delete All" />
<item
android:id="#+id/exitMenu"
android:title="Exit" />
</group >
<item
android:id="#+id/addMovieOffline"
android:title="Offline Mode" />
<item
android:id="#+id/addMovieOnline"
android:title="Online Mode" />
</menu>
For receiving events of the click on menu:
#override public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
// action needed
return true;
case R.id.item2:
// action needed
return true;
}
}
Use this
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.exitMenu:
//Your Logic
return true;
}
}
Use onOptionsItemSelected Method.
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.exitMenu:
//code here
return true;
}
return(super.onOptionsItemSelected(item));
}
Use switch case to identify the ids of menu items.
I've used a pretty standard implementation of the ShareActionProvider in my actionbar and it works fine. However, when I start another activity from an actionbutton in the actionbar and return from that activity the dropdownmenu of the SharedActionProvider is automatically opened.
This behavior doesn't occur when I execute the exact same code to open another activity from a normal button.
Here is my menu xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_btn_my_scedule"
android:icon="#drawable/ic_menu_my_calendar"
android:title="To Scedule"
android:showAsAction="ifRoom" />
<item android:id="#+id/menu_btn_share"
android:title="Share.."
android:showAsAction="ifRoom"
android:actionProviderClass="android.widget.ShareActionProvider" />
<item android:id="#+id/menu_btn_set_scedule"
android:icon="#drawable/ic_menu_today"
android:title="Set Scedule"
android:showAsAction="collapseActionView"
android:actionProviderClass="android.widget.ShareActionProvider" />
<item android:id="#+id/menu_btn_settings"
android:icon="#drawable/ic_menu_manage"
android:title="Settings"
android:showAsAction="collapseActionView"
android:actionProviderClass="android.widget.ShareActionProvider" />
<item android:id="#+id/menu_btn_feedback"
android:icon="#drawable/ic_menu_manage"
android:title="Feedback/Question"
android:showAsAction="collapseActionView"
android:actionProviderClass="android.widget.ShareActionProvider" />
<item android:id="#+id/menu_btn_about"
android:icon="#drawable/ic_menu_info_details"
android:title="About"
android:showAsAction="collapseActionView"
android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>
The other menu methods:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.optionsmenu, menu);
MenuItem actionItem = menu.findItem(R.id.menu_btn_share);
ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
actionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
actionProvider.setShareIntent(createShareIntent());
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return false;
case R.id.menu_btn_my_scedule:
Intent i = new Intent(ONTTOptions.this, ONTTShowScedule.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
startActivity(i);
return true;
case R.id.menu_btn_set_scedule:
i = new Intent(ONTTOptions.this, ONTTScedule.class);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
startActivity(i);
return true;
case R.id.menu_btn_settings:
i = new Intent(ONTTOptions.this, ONTTPreferences.class);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
startActivity(i);
return true;
case R.id.menu_btn_feedback:
showFeedbackDialog();
return true;
case R.id.menu_btn_about:
i = new Intent(ONTTOptions.this, ONTTAboutActivity.class);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
startActivity(i);
return true;
default: return super.onOptionsItemSelected(item);
}
}
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "https://play.google.com/store/apps/details?id=" + ONTTConfig.PACKAGE_NAME);
return shareIntent;
}
I must have missed something somewhere, thanks for any help.
I found the problem. I thought android:actionProviderClass="android.widget.ShareActionProvider"
was also needed on every actionbar item I wanted in the overflow menu. I simply removed it on every item except the share button ofcourse.
My menu xml now looks like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_btn_my_scedule"
android:icon="#drawable/ic_menu_my_calendar"
android:title="To Scedule"
android:showAsAction="ifRoom" />
<item android:id="#+id/menu_btn_share"
android:title="Share.."
android:showAsAction="ifRoom"
android:actionProviderClass="android.widget.ShareActionProvider" />
<item android:id="#+id/menu_btn_set_scedule"
android:icon="#drawable/ic_menu_today"
android:title="Set Scedule"
android:showAsAction="collapseActionView" />
<item android:id="#+id/menu_btn_settings"
android:icon="#drawable/ic_menu_manage"
android:title="Settings"
android:showAsAction="collapseActionView" />
<item android:id="#+id/menu_btn_feedback"
android:icon="#drawable/ic_menu_manage"
android:title="Feedback/Question"
android:showAsAction="collapseActionView" />
<item android:id="#+id/menu_btn_about"
android:icon="#drawable/ic_menu_info_details"
android:title="About"
android:showAsAction="collapseActionView" />
</menu>