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();
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'm working on this code
private void setupViews(){
frameLayout = (FrameLayout) findViewById(R.id.frame_id);
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav_id);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new ProfileFragment()).commit();
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
int navID = menuItem.getItemId();
switch (navID){
case R.id.home:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new HomeFragment()).commit();
break;
case R.id.search:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new SearchFragment()).commit();
break;
case R.id.profile:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new ProfileFragment()).commit();
break;
}
return true;
}
When I launch my app, it's automatically going on first bottom (I have 3 bottoms)
I want to change this to second bottom in navigation view. Please help me
Try this:
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav_id);
bottomNavigationView.setSelectedItemId(R.id.search);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new SearchFragment()).commit();
#iamhanniballake
I call setupViews() in main activity
I have 3 botoms
-1-2-3-
when my app running open fragment with a bottomnav view
its automatically selected 3 I want to change it to 2
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;
}
I am trying to implement a navigation drawer which switches among fragments and activities. Whenever I move to a activity window, the hamburger icon along with the navigation toolbar gets hidden which is why I cannot get back to my navigation drawer. How to resolve the issue?
This is my navigation code:
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch(item.getItemId()){
case R.id.nav_profile:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ProfileFragment()).commit();
break;
case R.id.nav_my_events:
Intent intent = new Intent(getBaseContext(), Main2Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
break;
case R.id.nav_settings:
Toast.makeText(this,"Settings..", Toast.LENGTH_LONG).show();
break;
case R.id.nav_logout:
Toast.makeText(this,"You logged out..", Toast.LENGTH_LONG).show();
break;
}
I am using Bottom Navigation View and in one of the options we display Firebase data, with a CardView.
But when I get out of this activity, and come back, either with the device back button or the activity arrow, it does not work. Does not display anything.
What could be wrong?
Prints:
Before:
After:
Code Bottom Navigation View:
BottomNavigationView bottomNavigationView =(BottomNavigationView) findViewById(R.id.bottomNavView_Bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(0);
menuItem.setChecked(true);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.ic_home:
break;
case R.id.ic_explore:
Intent intentExplore = new Intent(HomeActivity.this, ExploreActivity.class);
intentExplore.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentExplore);
break;
case R.id.ic_calendar:
Intent intentAgenda = new Intent(HomeActivity.this, AgendamentoActivity.class);
intentAgenda.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentAgenda);
break;
case R.id.ic_person:
Intent intentuser = new Intent(HomeActivity.this, OpcoesUsuarioActivity.class);
intentuser.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentuser);
break;
}
return false;
}
});
Note: In tests in activities without Bottom Navigation or with SlidingTabs, it works normally.
well you are using a wrong approach about the BottomNavigationView you need to use Fragment not Activities check this. About your problem when you are clicking on any item you puts your activity onPause that mean the activity still at the stack but is not visible, when clicking back you just finish the current activity and back to the last one with calling the onResume method the onCreate is not called, so put your RecyclerView work at your onResume method, or call finish() when leaving the activity.