I'm using a BottomNavigationView with 5 items, 4 Fragments and 1 Activity as in the image below
When any fragment is clicked I want it to act normally getting clicked and checked, but when the "+" item is clicked I'm opening an Activity but I don't want it to be checked, so I want it to be clickable so it can open the Activity, but I don't want to be checked, because when the user return back from the activity will see it selected even though it's the wrong selected item.
How can I do this?
Here's my code:
bottomNavigationView = findViewById(R.id.bottom_nav_view);
bottomNavigationView.setOnNavigationItemSelectedListener(this);
.
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
Fragment fragment = null;
switch (id) {
case R.id.home_nav_menu:
fragment = new HomeFragment();
break;
case R.id.inbox_nav_menu:
fragment = new InboxFragment();
break;
case R.id.add_nav_menu:
Intent intent = new Intent(this, AddActivity.class);
startActivity(intent);
return true;
case R.id.history_nav_menu:
fragment = new HistoryFragment();
break;
case R.id.profile_nav_menu:
fragment = new ProfileFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_place_holder, fragment).commit();
return true;
}
Seeing at the source code of the listener for the bottom navigation, you just need to return false when you don't want to display item as selected.
/**
* Listener for handling selection events on bottom navigation items.
*/
public interface OnNavigationItemSelectedListener {
/**
* Called when an item in the bottom navigation menu is selected.
*
* #param item The selected item
*
* #return true to display the item as the selected item and false if the item should not
* be selected. Consider setting non-selectable items as disabled preemptively to
* make them appear non-interactive.
*/
boolean onNavigationItemSelected(#NonNull MenuItem item);
}
So you simply return false in your case R.id.add_nav_menu: and it will work.
User MenuItem#setCheckable(boolean) API in order to play with menu item's checkable state.
MenuItem menuItem = navigation.getMenu().getItem(1);
menuItem.setCheckable(false);
Try like this:
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
Fragment fragment = null;
switch (id) {
case R.id.home_nav_menu:
fragment = new HomeFragment();
break;
case R.id.inbox_nav_menu:
fragment = new InboxFragment();
break;
case R.id.add_nav_menu:
menuItem.setCheckable(false);
Intent intent = new Intent(this, AddActivity.class);
startActivity(intent);
return true;
case R.id.history_nav_menu:
fragment = new HistoryFragment();
break;
case R.id.profile_nav_menu:
fragment = new ProfileFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_place_holder, fragment).commit();
return true;
}
Related
I am trying to build app with BottomNavigationView and I set setOnItemSelectedListener() method to bottom navigation so I can do what I want when user select one of the menu in bottom navigation.
everything is good when I don't set setOnItemSelectedListener(), but when I set setOnItemSelectedListener() method then the fragment is not updated automatically when user select the bottom navigation menu.
I consider if that do i have to handle fragment transaction manually when I set this method?
thanks ^^
Yes. You need to manually replace the fragment item on onNavigationItemSelected
Example:
private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
// By using switch we can easily get
// the selected fragment
// by using there id.
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.algorithm:
selectedFragment = new AlgorithmFragment();
break;
case R.id.course:
selectedFragment = new CourseFragment();
break;
case R.id.profile:
selectedFragment = new ProfileFragment();
break;
}
// It will help to replace the
// one fragment to other.
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, selectedFragment)
.commit();
return true;
}
};
You can find a good tutorial here: https://www.geeksforgeeks.org/bottomnavigationview-inandroid/
I have a bottom navigation bar with 4 fragments and 1 activity inside another activity(in which all fragment will be displayed). I want my first fragment to be displayed on the starting of the activity(in which all fragment will be displayed) along with the matching item of bottom navigation bar. My fragment 1 is getting displayed on starting but with wrong item of the bottom navigation bar.
This is what, I am getting on starting. Selected item should be Home(middle)
I have this under OnCreate
btmNav = findViewById(R.id.btmnav);
btmNav.setOnNavigationItemSelectedListener((navListner));
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new RecViewFragment()).commit();
and this outside Oncreate
private BottomNavigationView.OnNavigationItemSelectedListener navListner = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.navprofile:
getSupportFragmentManager().beginTransaction().
replace(R.id.fragment_container,new Fragment2()).commit();
break;
case R.id.navmap:
Intent intent = new Intent(MainActivityBuses.this, MapActivity.class);
startActivity(intent);
break;
case R.id.navhome:
getSupportFragmentManager().beginTransaction().
replace(R.id.fragment_container,new Fragment2()).commit();
break;
case R.id.navmybus:
getSupportFragmentManager().beginTransaction().
replace(R.id.fragment_container,new Fragment3()).commit();
break;
case R.id.navinfo:
getSupportFragmentManager().beginTransaction().
replace(R.id.fragment_container,new Fragment4()).commit();
break;
}
return true;
}
i got the answer,
just need to add this
btmNav.getMenu().findItem(R.id.navhome).setChecked(true);
below this
btmNav = findViewById(R.id.btmnav);
btmNav.setOnNavigationItemSelectedListener((navListner));
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new RecViewFragment()).commit();
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment;
switch (item.getItemId()) {
case R.id.navigation_lang:
toolbar.setTitle("Language");
fragment = new LangFragment();
loadFragment(fragment);
return true;
case R.id.navigation_paid:
toolbar.setTitle("Paid");
fragment = new PaidFragment();
loadFragment(fragment);
return true;
case R.id.navigation_unpaid:
toolbar.setTitle("UnPaid");
fragment = new UnpaidFragment();
loadFragment(fragment);
return true;
case R.id.navigation_declined:
toolbar.setTitle("Declined");
fragment = new DeclinedFragment();
loadFragment(fragment);
return true;
}
return false;
}
};
how to add badge on menu icon in fragment. I tried more examples but all are in activity i need to implement that in fragment. how to implement the xml for badge on menu. I added the image also. that is my tool bar menu.please help how to do that
The simplest way to do this is to use a library such as this
https://github.com/aurelhubert/ahbottomnavigation
you can easily show notification on bottom navigation icon
bottomNavigation.setNotification("1", 3);
Read more here: https://github.com/aurelhubert/ahbottomnavigation/blob/master/README.md
I created a Navigation Drawer and inserted the necessary items and their fragments.
Also, I added a item that I have to open another Activity, which code should I put that by pressing on the item do this?
For example, item id nav_menu6 ..... but it should not be linked to a fragment but open an activity ...
private void displaySelectedScreen(int itemId) {
Fragment fragment = null;
// Handle navigation view item clicks here.
switch (itemId) {
case R.id.nav_menu1:
fragment = new BlankFragment();
break;
case R.id.nav_menu2:
fragment = new BlankFragment2();
break;
case R.id.nav_menu3:
fragment = new BlankFragment3();
break;
case R.id.nav_menu4:
fragment = new BlankFragment4();
break;
case R.id.nav_menu5:
fragment = new BlankFragment5();
break;
case R.id.nav_menu6:
fragment = new BlankFragment6();
break;
}
https://developer.android.com/training/basics/firstapp/starting-activity.html
Read this about starting a new activity.
Basically, the key is the intent.
Intent intent = new Intent(this, YourActivityName.class);
startActivity(intent);
or in a shortened form:
startActivity(new Intent(this, YourActivityName.class));
I have implemented a ´NavigationView´ which has a few menu options that when user selects one, it changes the "main fragment" view to anothers...
My problem is, I'd like that when the user presses the back button, unless he is on the "main fragment" the screen returns to the "main fragment", otherwise close the app (regular onBackPressed() behaviour when there is no activity or backStack).
What I've done so far:
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.debitsNavId:
getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment_container, new MainFragment(), "main_fragment").commit();
break;
case R.id.friendsNavId:
getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment_container, new FriendFragment(), "friend_fragment").commit();
break;
case R.id.notificationsNavId:
getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment_container, new RequestFragment(), "request_fragment").commit();
break;
case R.id.configurationsNavId:
Intent i = new Intent(MainActivity.this,
SettingsActivity.class);
startActivity(i);
break;
case R.id.logoutNavId:
logout();
break;
default:
return false;
}
menuItem.setChecked(true);
drawer_layout.closeDrawers();
return false;
}
The problem is, if the user navigates from 'FriendFragment' to 'RequestFragment' or vice-versa, the fragment that gets "backstacked" is the previous one (either FriendFragment or RequestFragment) and I'd like to still be "MainFragment".
How can I achieve that ?
Hope I made myself clear and thanks for your time !
So basically what you would need to do is that when the MainFrag is visible set a boolean to true and if you navigate away set it to false.
Then override the onBackPressed, in your main activity, like the following method:
#Override
public void onBackPressed() {
if (!isMainFragVisible) {
getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment_container, new MainFragment(), "main_fragment").commit();
}
}
You can set the isMainFragVisible to true in this method:
case R.id.debitsNavId:
getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment_container, new MainFragment(), "main_fragment").commit();
isMainFragVisible = true
break;
And set it to false in any other navigation method.
do it this way
getSupportFragmentManager().beginTransaction().addToBackStack(fragment.getClass().getSimpleName()).replace(R.id.main_fragment_container, new MainFragment(), "main_fragment").commit();