I used BottomNavigationbar in my android app. When I run on a tablet, it shows very small icons in bottomnavigationbar. I want to increase the size for tablets.
I tried to put different sizes of icons in drawable folders but the problem is still there.
main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/backgroundGray"
tools:context="com.shakeel.board.MainActivity">
<FrameLayout
android:id="#+id/fragments_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottom_nav_bar">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_nav_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="#menu/bottom_navigation_menu"
android:padding="5dp"
android:background="#color/white">
</android.support.design.widget.BottomNavigationView>
bottom_navigation_menu.xml
<item android:id="#+id/nav_home"
android:title="Home"
android:icon="#drawable/home"/>
<item android:id="#+id/nav_favorites"
android:title="Favorite"
android:icon="#drawable/ic_favorite_red_24dp"/>
<item android:id="#+id/nav_shop"
android:title="Shop"
android:icon="#drawable/ic_shopping_basket_black_24dp"/>
<item android:id="#+id/nav_setting"
android:title="Setting"
android:icon="#drawable/ic_settings_black_24dp"/>
MainActivity.class
public class MainActivity extends AppCompatActivity {
BottomNavigationView bootomNavigation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bootomNavigation = findViewById(R.id.bottom_nav_bar);
BottomNavigationViewHelper.removeShiftMode(bootomNavigation);
getSupportFragmentManager().beginTransaction().replace(R.id.fragments_container, new HomeFragment()).commit();
bootomNavigation.setOnNavigationItemSelectedListener(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_favorites:
selectedFragment = new FavoritesFragment();
break;
case R.id.nav_shop:
selectedFragment = new ShopFragment();
break;
case R.id.nav_setting:
selectedFragment = new SettingFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragments_container, selectedFragment).commit();
return true;
}
});
}
}
You can download your icons from the material design page, select your icons and then select the Android option (see image below) and you will get a set of the same icon in different sizes for different screen sizes.
Related
I want to implement an interface having BottomNavigationView where clicking on the top search bar opens the next interface shown in figure 2. I have implemented BottomNavigationView. But unable to do that above said.
Here is the code:
public class HomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
BottomNavigationView bottomNavigationView = findViewById(R.id.home_bottom_nav_view);
bottomNavigationView.setSelectedItemId(R.id.home_recent_menu_id);
bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = new RecentFragment();
switch (item.getItemId()) {
case R.id.home_contact_menu_id:
selectedFragment = new ContactFragment();
break;
case R.id.home_recent_menu_id:
selectedFragment = new RecentFragment();
break;
case R.id.home_status_menu_id:
selectedFragment = new StatusFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.home_fragment_container_lyt_id, selectedFragment).commit();
return true;
}
});
}
}
Home Activity XML code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.SearchView
android:id="#+id/home_search_view_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<FrameLayout
android:id="#+id/home_fragment_container_lyt_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/home_search_view_id"
android:layout_above="#+id/home_bottom_nav_view"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/home_bottom_nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:menu="#menu/bottom_nav_menu"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
create a BottomNavigationFragment which is going to contain ViewPager2 with BottomNavigationView and that view pager will contain [favourite, recent and contacts ] fragments
create a SearchFragment which will be used for searching purpose
create a navigation graph which will contain [BottomNavigationFragment and SearchFragment] when user clicks on search view navigate him to SearchFragment
you can also set animation using Navigation Component lib
//adapter for ViewPager2 vp2 in BottomNavigationFragment
lets say your SearchView is in favourite fragment onClick or onFocus of that
findNavController().navigate(R.id.toSearchFragment)
you will land in Search fragment
I have made an app with bottom navigation view with Promo, Store, Reward, Coupon and Account tabs when I am changing from Coupon fragment to any other fragment, the bottom navigation view gets shrunk down as shown in the image, I tried changing layout width, height and converting coordinator layout to linear layout but it didn't help. The problem is occurring when I'm switching from home to any other tab only.
Layout File activity_main.xml
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.MainActivity">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="56dp"
android:text="#string/title_home"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:itemTextColor="#color/selector_bottom_navigation"
app:itemIconTint="#color/selector_bottom_navigation"
app:menu="#menu/navigation" />
Java File MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//loading the default fragment
loadFragment(new PromoFragment());
//getting bottom navigation view and attaching the listener
BottomNavigationView navigation = findViewById(R.id.navigation);
BottomNavigationViewUtils.disableShiftMode(navigation);
navigation.setOnNavigationItemSelectedListener(this);
}
#Override
public boolean onCreatePanelMenu(int featureId, Menu menu) {
getMenuInflater().inflate(R.menu.menu_wallet, menu);
return super.onCreatePanelMenu(featureId, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_wallet1:
return true;
case R.id.menu_qrcode:
Intent intent = new Intent(this, ScannerActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.navigation_promo:
fragment = new PromoFragment();
break;
case R.id.navigation_store:
fragment = new StoreFragment();
break;
case R.id.navigation_reward:
fragment = new RewardFragment();
break;
case R.id.navigation_coupon:
fragment = new CouponFragment();
break;
case R.id.navigation_account:
fragment = new AccountFragment();
break;
}
return loadFragment(fragment);
}
private boolean loadFragment(Fragment fragment) {
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();
return true;
}
return false;
}
This is the picture I meant:
I previously added SearchView to the coupon fragment and fragment store
It turns out that if you are using a coordinator layout and viewpager inside a fragment, you will notice that the viewpager extends the screen a bit. Just disable the scroll features of the coordinator layout inside the fragment and you will notice the bottom bar doesn't get shrunk. Weird I know, but it works.
I add android:fitsSystemWindows="false" in CoordinatorLayout and it worked
Find android:fitsSystemWindows="true" in your fragment xml and remove this line
or change it to android:fitsSystemWindows="false". Your problem will be solved.
I'm trying to create a bottom navigation bar with 5 items regularly spaced and without labels underneath the icons. I found lots of guidance and followed it but unlike the examples I followed, my navigation bar display is very uneven. How can I make it evenly distributed?
This is what it looks like now:
My xml view (within Relative Layout):
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="#color/colorPrimary"
app:itemIconTint="#android:color/white"
app:itemTextColor="#android:color/white"
app:menu="#menu/navigation" />
Navigation xml file:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/navigation_home"
android:enabled="true"
android:icon="#drawable/ic_home_black_24dp"
android:title="Home"
app:showAsAction="always" />
<item
android:id="#+id/navigation_look"
android:icon="#drawable/ic_find_in_page_black_24dp"
android:title="Search"
android:enabled="true"
app:showAsAction="always"/>
<item
android:id="#+id/navigation_basket"
android:enabled="true"
android:icon="#drawable/ic_shopping_basket_black_24dp"
android:title="Basket"
app:showAsAction="always"/>
<item
android:id="#+id/navigation_favourite"
android:icon="#drawable/ic_favorite_black_24dp"
android:title="Favourite"
android:enabled="true"
app:showAsAction="always"/>
<item
android:id="#+id/navigation_account"
android:enabled="true"
android:icon="#drawable/ic_person_black_24dp"
android:title="Account"
app:showAsAction="always"/>
</menu>
And this is what is in my CategoryActivity.java within onCreate:
BottomNavigationView navigationSearch = (BottomNavigationView)
findViewById(R.id.navigation_search);
navigationSearch.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
Intent main = new Intent(CategoryActivity.this, MainActivity.class);
startActivity(main);
break;
case R.id.navigation_look:
Intent search = new Intent(CategoryActivity.this, SearchActivity.class);
startActivity(search);
break;
case R.id.navigation_basket:
Intent basket = new Intent(CategoryActivity.this, BasketActivity.class);
startActivity(basket);
break;
case R.id.navigation_favourite:
Intent favourite = new Intent(CategoryActivity.this, FavouritesActivity.class);
startActivity(favourite);
break;
case R.id.navigation_account:
Intent account = new Intent(CategoryActivity.this, AccountActivity.class);
startActivity(account);
break;
}
return false;
}
});
i had the same problem but with 4 items, there is a sort of trick, you should use this method on your BottomNavigationView after the findViewById.
#SuppressLint("RestrictedApi")
public static void disableShiftMode(BottomNavigationView view) {
BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
try {
Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
shiftingMode.setAccessible(true);
shiftingMode.setBoolean(menuView, false);
shiftingMode.setAccessible(false);
for (int i = 0; i < menuView.getChildCount(); i++) {
BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
//noinspection RestrictedApi
item.setShiftingMode(false);
// set once again checked value, so view will be updated
//noinspection RestrictedApi
item.setChecked(item.getItemData().isChecked());
}
} catch (NoSuchFieldException e) {
Log.e("BNVHelper", "Unable to get shift mode field", e);
} catch (IllegalAccessException e) {
Log.e("BNVHelper", "Unable to change value of shift mode", e);
}
}
Sorry, i can't really explain the solution more in-depth since it was an issue i solved a long time ago following some advice on StackOverflow and i don't remember the theory/reasons there was a need for this, but this works fine for me on Android Oreo so you could do some experiments and find a better way.
I recommend you to use the fragments
navigationSearch.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId())
{
case R.id.navigation_home:
selectedFragment = HomeFragment.newInstance();
break;
case R.id.navigation_look:
selectedFragment = LookFragment.newInstance();
break;
case R.id.navigation_basket:
selectedFragment = BasketFragment.newInstance();
break;
case R.id.navigation_favourite:
selectedFragment = FavouriteFragment.newInstance();
break;
case R.id.navigation_account:
selectedFragment = AccountFragment.newInstance();
break;
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout,selectedFragment);
transaction.commit();
return true;
}
});
setDefaulFragment();
private void setDefaulFragment() {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout,HomeFragment.newInstance());
transaction.commit();
}
activity_category.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient"
tools:context=".CategoryActivity">
<FrameLayout
android:id="#+id/frame_layout"
android:animateLayoutChanges="true"
android:layout_above="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_alignParentBottom="true"
android:background="#color/colorPrimaryDark"
app:itemIconTint="#drawable/nav_item_color_state"
app:itemTextColor="#drawable/nav_item_color_state"
app:menu="#menu/bottom_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
Example fragment:
HomeFragment.java:
public class HomeFragment extends Fragment {
View mFragment;
public static HomeFragment newInstance(){
HomeFragment homeFragment = new HomeFragment();
return homeFragment ;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
mFragment = inflater.inflate(R.layout.fragment_home,container,false);
return mFragment;
}
}
Example fragment :
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeFragment">
<TextView
android:text="Home Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
What you can do is give the Title attribute as android:title="" in Navigation.xml and then to disable the shift mode use BottomNavigationViewHelper.disableShiftMode(your BottomNavigationView)
Use a linear layout with horizontal orientation.
You can give layout_weight for each item about 20. WeightSum of 100 for the Linear layout.
Load the data using fragments.
I have a Bottom Navigation View with 4 items.
I wish to set the items in the middle of each bottom navigation view button and I have found a method but it works only when the application starts.
After I select another item, every icon moves up and I don't know why they are not keeping their position.
public class MainActivity extends AppCompatActivity{
private Toolbar mToolbar;
private BottomNavigationView bottomNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.customToolbar);
setSupportActionBar(mToolbar);
setTitle("");
mToolbar.setNavigationIcon(R.mipmap.back_icon);
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
BottomNavigationViewShiftDisable.disableShiftMode(bottomNavigationView);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.profile:
fragment = new FirstFragment();
break;
case R.id.friends:
fragment = new SecondFragment();
break;
case R.id.circle:
fragment = new ThirdFragment();
break;
case R.id.settings:
fragment = new ForthFragment();
break;
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, fragment);
transaction.commit();
setMenuIconsInMiddle(bottomNavigationView);
return true;
}
});
bottomNavigationView.setSelectedItemId(R.id.profile);
}
/**
* This method is used to set margins for all the icons in the menu that is used in the
* bottom navigation view.
* #param navigationView an instance of the Bottom navigation view that holds the menu with
* the icons.
*/
public void setMenuIconsInMiddle(BottomNavigationView navigationView){
BottomNavigationMenuView menuView = (BottomNavigationMenuView)navigationView.getChildAt(0);
for(int index = 0; index <menuView.getChildCount(); index++){
final View iconView = menuView.getChildAt(index).findViewById(android.support.design.R.id.icon);
final ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams)iconView.getLayoutParams();
layoutParams.setMargins(0,50,0,0);
navigationView.requestLayout();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hermes.profilescreen.MainActivity">
<include layout="#layout/custom_toolbar"/>
<include layout="#layout/bottom_navigation"/>
</RelativeLayout>
bottom_nav_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:id="#+id/profile"
android:enabled="true"
android:icon="#drawable/ic_people_outline_black_24dp"
app:showAsAction="ifRoom"
android:title=""
/>
<item
android:id="#+id/friends"
android:enabled="true"
android:icon="#drawable/ic_person_black_24dp"
app:showAsAction="ifRoom"
android:title=""
/>
<item
android:id="#+id/circle"
android:enabled="true"
android:icon="#drawable/ic_panorama_fish_eye_black_24dp"
app:showAsAction="ifRoom"
android:title=""
/>
<item
android:id="#+id/settings"
android:enabled="true"
android:icon="#drawable/setting_white"
app:showAsAction="ifRoom"
android:title=""
/>
</menu>
bottom_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/frame_layout"
android:layout_above="#+id/bottom_navigation"
>
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/light_black"
app:itemBackground="#drawable/set_backgorund"
app:itemIconTint="#color/item_state"
app:menu="#menu/bottom_nav_main"
/>
</RelativeLayout>
call setMenuIconsInMiddle on a thread. try this:-
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
//your code here
...
...
...
//Looper.getMainLooper() runs on main thread
new Handler(Looper.getMainLooper()).post(new Runnable(){
#Override
public void run() {
setMenuIconsInMiddle(bottomNavigationView);
}
});
return true;
}
});
this is working for me...
I hope I'm explaining this correctly. Basically I have a basic Android app with navigation bar on the left, and I'm trying to make the content page change layout based on what I select there.
What I did was to include both layouts on the application main layout, but when I try selecting from the nav bar, the layouts are on top of each other.
Can I get a pointer of to make them not to lay on top of each other? Am I approaching this correctly?
Here's my xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.test.justforfun.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<include layout="#layout/content_second"/>
</android.support.design.widget.CoordinatorLayout>
Show one layout one time, and show another when you select sth.
Set the visibility to GONE when you need to hide the layout. And then again to VISIBLE when you want to show it.
You need to use Fragment instead of hiding and showing layout/views. Set listeners on your Navigation Bar and use FragmentManager to replace your current fragment.
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
selectDrawerItem(menuItem);
return true;
}
});
}
public void selectDrawerItem(MenuItem menuItem) {
// Create a new fragment and specify the fragment to show based on nav item clicked
Fragment fragment = null;
Class fragmentClass;
switch(menuItem.getItemId()) {
case R.id.nav_first_fragment:
fragmentClass = FirstFragment.class;
break;
case R.id.nav_second_fragment:
fragmentClass = SecondFragment.class;
break;
case R.id.nav_third_fragment:
fragmentClass = ThirdFragment.class;
break;
default:
fragmentClass = FirstFragment.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
// Highlight the selected item has been done by NavigationView
menuItem.setChecked(true);
// Set action bar title
setTitle(menuItem.getTitle());
// Close the navigation drawer
mDrawer.closeDrawers();
}
source: https://guides.codepath.com/android/Fragment-Navigation-Drawer