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
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 am developing a car rental app and right now I have 3 fragments in my app (Home - where all the cars are displayed from a recyclerview , Orders - where user's bookings are shown , Profile - user profile). I can switch beetween these 3 fragments from my bottom navigationbar.
Now I created onclick listeners for every car on home page. When I click on a car it sends me to a new activity "activity_car_detail.xml" which contains specific information about the selected car. My question is: how can I make the navigation bar to appear on this Car Detail activity? Should I use fragment instead of activity or what should I do?
This is the code for the Main Activity which contains the bottom navigation bar:
public class MainActivity extends AppCompatActivity {
public static ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.nav_home:
selectedFragment = new HomeFragment();
break;
case R.id.nav_orders:
selectedFragment = new OrdersFragment();
break;
case R.id.nav_profile:
selectedFragment = new ProfileFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();
return true;
}
};
}
I suggest using the Navigation component instead of Fragment transactions. Also this will fix your issue and it will remove boilerplate code. Read more about it's documentation here.
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
In my app I have 5 fragments. In order to switch between them i use bottom navigation bar.
Also, i need to open activities from my fragments, but when I open new activity it opens in each of the fragments. (I mean over the fragments, in each case)
How to make the activity open in one case?
My code:
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.nav_home:
fragmentManager.beginTransaction().hide(active).show(fragment1).commit();
active = fragment1;
return true;
case R.id.nav_bookmark:
fragmentManager.beginTransaction().hide(active).show(fragment2).commit();
active = fragment2;
return true;
case R.id.nav_blog:
fragmentManager.beginTransaction().hide(active).show(fragment3).commit();
active = fragment3;
return true;
case R.id.nav_notification:
fragmentManager.beginTransaction().hide(active).show(fragment4).commit();
active = fragment4;
return true;
case R.id.nav_account:
fragmentManager.beginTransaction().hide(active).show(fragment5).commit();
active = fragment5;
return true;
}
return false;
}
};
Thanks!
This is clearly a FragmentManager issue - and not an Activity issue.
Instead .hide().show() use .replace().
For reference, see the documentation.
According to the Android documentation, an activity is always placed on top of the current activity stack.
So if you have the MainActivity with the BottomNavigationView and the fragments, starting a new Activity will open the activity above the MainActivity with the BottomNavigationView. So it is not possible to open an Activity only in one of the BottomNavigationView Fragments.
To achieve the desired behaviour, consider using another Fragment instead.
I created an activity with a bottom navigation bar.
I googled a lot things about it but now I don't know how to handle this exactly.
Before, I just started another activity when the user clicks the bottom navigation but I think it's not good.
How can I switch between the tabs?
Do I have to work with fragments? And what about 'setContentView(int layoutResID)'? How can I do that? I'm confused...
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
return true;
case R.id.navigation_dashboard:
startActivity(dashboardActivity);
return true;
case R.id.navigation_notifications:
startActivity(dashboardActivity);
return true;
}
return false;
}
};
Thank you very much for your help - I hope, you understood what I mean.
Activity transition is always expensive and we should switch from one activity to another only when we are switching the context. A fragment is a portion of UI in an activity. Same fragment can be used with multiple activities. Just like activity a fragment has its own lifecycle and setContentView(int layoutResID) can be set to different layout in OnCreate of fragment.
This link explains more on when to use activity or fragment.
Android developer guide on Fragments
Code path tutorial on bottom navigation views.
Please refer to :-
https://github.com/waleedsarwar86/BottomNavigationDemo
and complete explanation in
http://waleedsarwar.com/posts/2016-05-21-three-tabs-bottom-navigation/
You will get a running code with the explanation here.
Bottom Navigation View is a navigation bar introduced in android library to make it easy to switch between views with a single tap. It can although be used for almost any purpose, but is most commonly used to switch between fragments with a single tap. Its use for opening activities is somewhat absurd, since it ignores its most important functionality of switching the views with a single tap. There are many good articles and blogs out there in this regard, one of which is:
https://medium.com/#hitherejoe/exploring-the-android-design-support-library-bottom-navigation-drawer-548de699e8e0
Hope this solves your doubt..
bottomNavigationView.setOnNavigationItemSelectedListener
(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.action_item1:
selectedFragment = ItemOneFragment.newInstance();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, selectedFragment);
transaction.commit();
// selectedFragment.getChildFragmentManager().beginTransaction();
break;
case R.id.action_item2:
selectedFragment = ItemTwoFragment.newInstance();
FragmentTransaction transactiona = getSupportFragmentManager().beginTransaction();
transactiona.replace(R.id.frame_layout, selectedFragment);
transactiona.commit();
// selectedFragment = ItemThreeFragment.newInstance();
break;
case R.id.action_item3:
// selectedFragment = ItemOneFragment.newInstance();
Intent intent=new Intent(MainView.this, YoutActivityLive.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
// selectedFragment = ItemTwoFragment.newInstance();
break;
case R.id.action_item5:
selectedFragment = ItemOneFragment.newInstance();
FragmentTransaction transactionb = getSupportFragmentManager().beginTransaction();
transactionb.replace(R.id.frame_layout, selectedFragment);
transactionb.commit();
// selectedFragment = ItemFiveFragment.newInstance();
break;
}
return true;
}
});