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;
}
});
}
Related
Can anyone help me, my bottom navigation bar looks like this:
I want to make it to look like this:
Offcourse, with blue color.
My code is below: Thanks everyone!!
layout.xml
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="#menu/main_manu"
app:itemBackground="#color/blue"
app:itemIconTint="#android:color/white"
app:itemTextColor="#android:color/white"
app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"
/>
Application.java
final BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(2);
menuItem.setChecked(true);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_pocetna:
new ListaVoznji.Home().execute();
break;
case R.id.action_unos:
new ListaVoznji.Login().execute();
break;
case R.id.action_pregled:
Intent intent2 = new Intent(ListaVoznji.this,ListaSvihVoznji.class);
intent2.putExtra("voznja",voznja);
startActivity(intent2);
break;
case R.id.action_shutdown:
Intent homescreen=new Intent(ListaVoznji.this,LoginActivity.class);
log = 1;
homescreen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homescreen);
finish();
break;
}
return true;
}
});
Add app:labelVisibilityMode="labeled" in your layout in botton navigation
Just use the background attribute to change the whole background , and if you want to customize the item's text and icon color , you can do this by :
First create a drawable file name item_background , add to it the following lines :
<selector>
<item android:state_checked="true" android:color="#color/colorPrimary" />
<item android:state_checked="false" android:color="#color/colorAccent"/>
</selector>
Then in your bottom navigation view add attributes itemTextColor & itemIconTint with the value #drawable/item_background
I have this menu that pops up fine when I create it.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/day"
android:title="Day" />
<item
android:id="#+id/week"
android:title="Week" />
<item
android:id="#+id/month"
android:title="Month" />
<item
android:id="#+id/year"
android:title="Year" />
</menu>
For some reason, the onMenuItemClick is only giving me "Year" no matter which item I click on. This is a really strange problem and I have no idea why it's happening.
public void showMenu(View v) {
PopupMenu popup = new PopupMenu(getContext(), v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.date_range_menu, popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.day:
((MainActivity) requireActivity()).toast("Day");
changeDateRange(DateRange.DAY);
case R.id.week:
((MainActivity) requireActivity()).toast("Week");
changeDateRange(DateRange.WEEK);
case R.id.month:
((MainActivity) requireActivity()).toast("Month");
changeDateRange(DateRange.MONTH);
case R.id.year:
((MainActivity) requireActivity()).toast("Year");
changeDateRange(DateRange.YEAR);
default:
return false;
}
}
});
}
You need to add the break; keyword to each case.
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 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;
}
});