This links contains the screenshot. It shows the dark places inplace of the icons. The shape of those places is just like that of the icons but icons are not appearing.
MainActivity.java
[private BottomNavigationView.OnNavigationItemSelectedListener
mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_buy:
fragment = new Buy();
break;
case R.id.navigation_orders:
fragment = new Orders();
break;
case R.id.navigation_navigation:
fragment = new Navigation();
break;
case R.id.navigation_chat:
fragment = new Chat();
break;
case R.id.navigation_invite:
fragment = new Invite();
break;
}
final FragmentTransaction transaction =
fragmentManager.beginTransaction();
transaction.replace(R.id.content, fragment).commit();
return true;
}
};]
menu.xml
[<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/navigation_buy"
android:icon="#drawable/buy_ic"
android:title="#string/title_buy"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/navigation_orders"
android:icon="#drawable/orders_ic"
android:title="#string/title_orders"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/navigation_navigation"
android:icon="#drawable/navigation_ic"
android:title="#string/title_navigation"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/navigation_chat"
android:icon="#drawable/chat_ic"
android:title="#string/title_chat"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/navigation_invite"
android:icon="#drawable/invite_ic"
android:title="#string/title_invite"
app:showAsAction="ifRoom"/>
</menu>]
activity_main.xml
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="#color/colorPrimary"
app:itemTextColor="#color/white"
app:menu="#menu/navigation" />
Use itemIconTint instead of app:itemBackground="#color/colorPrimary"
app:itemIconTint="#color/colorPrimary"
Related
It would be even better if the text appears below the respective icon while the icons are evenly distributed.
Currently, the items appear stacked in landscape mode.
Current implementation:
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav_bar"
style="#style/Widget.MaterialComponents.BottomNavigationView.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemIconTint="#color/selector_icon_tint_bottom_nav"
app:itemTextColor="#color/selector_icon_tint_bottom_nav"
app:labelVisibilityMode="labeled"
app:menu="#menu/bottom_nav" />
Try this out I hope it will work.Its working for me
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/colorPrimary"
app:labelVisibilityMode="labeled"
app:itemIconTint="#color/white"
app:itemTextColor="#color/white"
app:menu="#menu/bottom_navigation_menu" />
</FrameLayout>
Make a menu directory under res and in that directory make an XML layout
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/navigation_home"
android:icon="#drawable/ic_home"
android:title="Home" />
<item
android:id="#+id/navigation_profile"
android:icon="#drawable/ic_profile"
android:title="Profile" />
<item
android:id="#+id/navigation_about"
android:icon="#drawable/ic_aboutUs"
android:title="About Us" />
<item
android:id="#+id/navigation_logout"
android:icon="#drawable/ic_logout"
android:title="Logout" />
</menu>
In main activity implement BottomNavigationView.OnNavigationItemSelectedListener and implements its all method then you can do your desire action on each icon
bottomNavigationView = findViewById(R.id.bottom_navigation_view);
bottomNavigationView.setOnNavigationItemSelectedListener(this);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
Toast.makeText(this, "Home", Toast.LENGTH_SHORT).show();
return true;
case R.id.navigation_profile:
Toast.makeText(this, "Profile", Toast.LENGTH_SHORT).show();
return true;
case R.id.navigation_about:
Toast.makeText(this, "About Us", Toast.LENGTH_SHORT).show();
return true;
case R.id.navigation_logout:
Toast.makeText(this, "Logout", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
Edited :For video tutorial you can also see this tutorial
(https://www.youtube.com/watch?v=oeKtwd1DBfg)
The first item in the navigation bar keep highlighting.
When I click other item on the navigation bar, the content will change but the corresponding item will not be highlighted, just keep highlighting the first item.
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment=null;
switch (item.getItemId()){
case R.id.number:
fragment=new data();
break;
case R.id.graph:
fragment=new graph();
break;
}
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_drawer, fragment);
fragmentTransaction.commit();
return true;
}
});
this is the listener
<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.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimaryDark"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
app:menu="#menu/navigation" />
<LinearLayout
android:id="#+id/graphlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/navigation"
android:orientation="vertical"
android:gravity="center_vertical">
</LinearLayout>
</RelativeLayout>
this is the xml
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/number"
android:title="number"
android:checkable="true"/>
<item
android:id="#+id/graph"
android:title="graph"
android:checkable="true"/>
</menu>
this is the meun.xml
This usually happens beacause of when onNavigationItemSelected method return false value. You can check this with remove all code except return true inside the onNavigationItemSelected method. Just like this;
bottomNavigationView.setOnNavigationItemSelectedListener(new
BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
return true;
}
});
Then you'll see it works but the content was not changed. For change with content, If I were you I'll change the default return value as false like this;
bottomNavigationView.setOnNavigationItemSelectedListener(new
BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.number:
//Open fragment or do whatever you want.
return true;
case R.id.graph:
//Open another fragment or do whatever you want.
return true;
}
return false;
}
});
It is included in the Design Support Library, starting with version 25.0.0. You can include it in your build.gradle file with the following line (you'll also need the AppCompat Support Library as the Design Support Library's dependency):
First use girdle compile both the girdle in your project
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:design:25.0.0'
then create xml layout:
<android.support.design.widget.BottomNavigationView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemBackground="#color/darkGrey"
app:itemIconTint="#color/bottom_navigation_item_background_colors"
app:itemTextColor="#color/bottom_navigation_item_background_colors"
app:menu="#menu/menu_bottom_navigation" />
Create a resource file just like a Navigation Drawer or an Overflow menu:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_one"
android:icon="#android:drawable/ic_dialog_map"
android:title="One"/>
<item
android:id="#+id/action_two"
android:icon="#android:drawable/ic_dialog_info"
android:title="Two"/>
<item
android:id="#+id/action_three"
android:icon="#android:drawable/ic_dialog_email"
android:title="Three"/>
<item
android:id="#+id/action_four"
android:icon="#android:drawable/ic_popup_reminder"
android:title="Four"/>
</menu>
display image below:
To achieve different tinting for selected items, you should specify a color list resource as itemBackground and itemTextColor like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:color="#color/colorAccent"
android:state_checked="false"/>
<item
android:color="#android:color/white"
android:state_checked="true"/>
</selector>
Finally, you can listen to tab selection events by adding an BottomNavigation.OnNavigationItemSelectedListener via the setOnNavigationItemSelectedListener() method:
bottomNavigationView.setOnNavigationItemSelectedListener(new
BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.action_one:
// Switch to page one
break;
case R.id.action_two:
// Switch to page two
break;
case R.id.action_three:
// Switch to page three
break;
}
return true;
}
});
try this code.
I have a drawer layout that needs to have two navigation views in it, because there's a couple of options that need to be aligned on top and another on the bottom, with a divider separating them.
I've managed to make it work, but now I have two different problems with my setup.
1) The first one, I can't change the selected items text color (the background color works as intented via a drawer selector, but for some reason, the selected item's text color is always colorPrimary)
2) The second one is trickier, since there's two navigation views, it can happen that there's a selected row on the first and on the second one, even though they all work loading fragments in the same container. Is there a way to manage that, when an item for the first navigation view is selected, the second navigation view's selected item gets deselected?
This is the drawer's layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- 1º Layout de la activity -->
<include layout="#layout/activity_main"/>
<!-- 2º Layout del drawer -->
<android.support.design.widget.NavigationView
android:id="#+id/container_navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemBackground="#drawable/drawer_list_selector"
android:nestedScrollingEnabled="true"
android:scrollIndicators="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="match_parent"
android:layout_height="#dimen/divider_height"
android:background="#color/colorAccent"
android:layout_above="#+id/navigation_view"
android:id="#+id/top_separator"/>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_above="#+id/bottom_separator"
app:itemBackground="#drawable/drawer_list_selector"
app:menu="#menu/menu_navigation" />
<View
android:layout_width="match_parent"
android:layout_height="#dimen/divider_height"
android:background="#color/colorAccent"
android:layout_above="#+id/navigation_bottom"
android:id="#+id/bottom_separator"/>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:menu="#menu/menu_navigation_bottom"
app:itemBackground="#drawable/drawer_list_selector"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
This is the top navigation view's layout:
<?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">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_tus_ofertas"
android:title="#string/tus_ofertas"
android:icon="#drawable/ic_offer" />
<item
android:id="#+id/nav_movimiento_puntos"
android:title="#string/movimiento_puntos"
android:icon="#drawable/ic_points"/>
<item
android:id="#+id/nav_asociaciones"
android:title="#string/asociaciones"
android:icon="#drawable/ic_flag"/>
</group>
</menu>
Bottom navigation view's layout:
<?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_terminos"
android:title="#string/terminos"
android:icon="#drawable/ic_document" />
<item
android:id="#+id/nav_contacto"
android:title="#string/contacto"
android:icon="#drawable/ic_email"/>
<item
android:id="#+id/nav_valorar"
android:title="#string/valorar"
android:icon="#drawable/ic_rate_review"/>
<item
android:id="#+id/nav_exit"
android:title="#string/exit"
android:icon="#drawable/ic_shutdown"/>
</group>
</menu>
Selected item's background selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/colorAccent" />
<item android:state_activated="true" android:drawable="#color/colorAccent" />
<item android:state_selected="true" android:drawable="#color/colorAccent" />
<item android:state_checked="true" android:drawable="#color/colorAccent" />
<item android:state_pressed="false" android:drawable="#android:color/transparent" />
<item android:state_activated="false" android:drawable="#android:color/transparent" />
<item android:state_selected="false" android:drawable="#android:color/transparent" />
<item android:state_checked="false" android:drawable="#android:color/transparent" />
</selector>
The activity that calls the drawer and handles the events:
public class MainActivity extends AppCompatActivity {
#Bind(R.id.toolbar) Toolbar toolbar;
#Bind(R.id.tabs) TabLayout tabs;
#Bind(R.id.drawer_layout) DrawerLayout drawerLayout;
#Bind(R.id.container_navigation) NavigationView containerNavigator;
#Bind(R.id.navigation_view) NavigationView navigationView;
#Bind(R.id.navigation_bottom) NavigationView navigationBottom;
#Bind(R.id.vsFooter) ViewStubCompat stub;
ImageView userPicture;
TextView userMail;
ViewPager viewPager;
Menu menu;
ActionBar actionBar;
ViewPagerFragment viewPagerFragment;
Usuario currentUser;
public Usuario getCurrentUser() {
return currentUser;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
ButterKnife.bind(this);
currentUser = UserApiManager.getInstance(getApplicationContext()).getCurrentUser();
viewPagerFragment = ViewPagerFragment.newInstance();
setupToolbar();
setupNavigationDrawer();
ApiManager.getInstance(getApplicationContext()).setupFooter(stub, currentUser.getIdLocalidad(), this);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,viewPagerFragment).commit();
}
public void setupViewPager(ViewPager viewPager) {
tabs.setupWithViewPager(viewPager);
}
private void setupToolbar() {
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
if (actionBar!=null){
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_black);
actionBar.setTitle(getString(R.string.app_title));
}
}
private void setupNavigationDrawer() {
View headerView = navigationView.inflateHeaderView(R.layout.navigation_header);
userPicture = (ImageView) headerView.findViewById(R.id.user_picture);
userMail = (TextView) headerView.findViewById(R.id.user_mail);
userMail.setText(currentUser.getEmail());
Picasso.with(this).load(currentUser.getImagenURL()).transform(new CircleTransformation()).into(userPicture);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_tus_ofertas:
showTabLayout();
actionBar.setTitle(getString(R.string.app_title));
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,viewPagerFragment).commit();
break;
case R.id.nav_movimiento_puntos:
hideTabLayout();
Bundle bundle = new Bundle();
bundle.putString("idcliente", currentUser.getId());
MovimientoPuntosFragment movimientoPuntosFragment = MovimientoPuntosFragment.newInstance();
movimientoPuntosFragment.setArguments(bundle);
actionBar.setTitle(getString(R.string.movimiento_puntos));
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, movimientoPuntosFragment).commit();
break;
case R.id.nav_asociaciones:
hideTabLayout();
actionBar.setTitle(getString(R.string.asociaciones));
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, AsociacionFragment.newInstance()).commit();
break;
}
drawerLayout.closeDrawers();
return true;
}
});
navigationBottom.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_terminos:
hideTabLayout();
actionBar.setTitle(getString(R.string.terminos));
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, LegalesFragment.newInstance()).commit();
break;
case R.id.nav_contacto:
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("mailto:?subject=" + getString(R.string.contacto_email));
intent.setData(data);
startActivity(intent);
break;
case R.id.nav_valorar:
final String appPackageName = BuildConfig.APPLICATION_ID;
try {
Log.i("url", "market://details?id=" + appPackageName);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
break;
case R.id.nav_exit:
break;
}
drawerLayout.closeDrawers();
return true;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_over, menu);
this.menu = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
break;
case R.id.action_user:
Intent intent = new Intent(this, PerfilUsuarioActivity.class);
startActivity(intent);
break;
}
return super.onOptionsItemSelected(item);
}
public void hideTabLayout(){
tabs.setVisibility(View.GONE);
}
public void showTabLayout(){
tabs.setVisibility(View.VISIBLE);
}
}
Any help would be appreciated!
Thanks in advance!
Solved!
The problem with the color I solved it with a selector, it didn't work before because I had a syntax error (selector tag inside the main selector tag).
This is the color selector's code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#FFFFFFFF" android:state_checked="true" />
<item android:color="#E6000000" android:state_checked="false"/>
</selector>
The second problem I solved it by programmatically deselecting the other navigationview's items onNavigationItemSelected.
This is the function I call:
public void deselectItems(NavigationView view){
int size = view.getMenu().size();
for (int i = 0; i < size; i++) {
view.getMenu().getItem(i).setChecked(false);
}
}
I know there must be a better way to solve the second problem, but this one worked.
I'm new student on xamarin android. so I don't know how to create a event click on that.
<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_48dp"
android:title="Home" />
<item
android:id="#+id/nav_genre"
android:icon="#drawable/ic_toc_black_48dp"
android:title="Genres" />
<item
android:id="#+id/nav_audio"
android:icon="#drawable/ic_settings_input_antenna_black_48dp"
android:title="Audio" />
<item
android:id="#+id/nav_download"
android:icon="#drawable/ic_get_app_black_48dp"
android:title="Download" />
</group>
<item android:title="Account">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_lock_open_black_48dp"
android:title="About"/>
<item
android:id="#+id/nav_signout"
android:icon="#drawable/ic_perm_identity_black_48dp"
android:title="Sign out"/>
</group>
</menu>
</item>
</menu>
<!-- your content layout -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:titleTextColor="#android:color/background_light" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<frameLayout
android:id:="#+id/frameContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
</LinearLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="#+id/nav_view"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/navmenu" />
</android.support.v4.widget.DrawerLayout>
I want to when click that item 1 -> fragmenthome(there only listview)
I want to when click that item 2 -> fragmentgenres(there only listview)
They all be showing up in frameContainer.
This is how to handle click events and load fragments accordingly.
HomeFragment homFragment;
GenresFragment genresFragment;
int currentFragmentId=Resource.Id.nav_home;
Above declarations are to be made global in the Activity.
var navigationView = FindViewById<NavigationView> (Resource.Id.nav_view);
navigationView.NavigationItemSelected+= NavigationView_NavigationItemSelected;
CreateFragments ();
LoadInditialFragment ();
Add the above snippets in OnCreate.
void CreateFragments()
{
homeFragment = new HomeFragment ();
genresFragment = new GenresFragment ();
}
void LoadInditialFragment()
{
var transaction = FragmentManager.BeginTransaction ();
transaction.Add (Resource.Id.frameContainer, genresFragment).Hide(genresFragment);
transaction.Add (Resource.Id.frameContainer, homeFragment);
transaction.Commit ();
}
void NavigationView_NavigationItemSelected (object sender, NavigationView.NavigationItemSelectedEventArgs e)
{
if (e.MenuItem.ItemId != currentFragmentId)
SwitchFragment (e.MenuItem.ItemId);
drawerLayout.CloseDrawers ();
}
void SwitchFragment(int FragmentId)
{
var transaction = FragmentManager.BeginTransaction ();
switch (currentFragmentId)
{
case Resource.Id.nav_home:
transaction.Hide (homeFragment).Commit ();
break;
case Resource.Id.nav_genre:
transaction.Hide (genresFragment).Commit ();
break;
}
transaction = FragmentManager.BeginTransaction ();
switch (FragmentId)
{
case Resource.Id.nav_home:
transaction.Show (homeFragment);
transaction.Commit ();
break;
case Resource.Id.nav_genre:
transaction.Show (genresFragment);
transaction.Commit ();
break;
}
currentFragmentId = FragmentId;
}
In the Create Fragment Method All Fragments are instantiated initially and attached to the Fragment. Then all fragments except the fragment to be shown are hidden. Then as the user clicks on an item in NavigationView the current fragment is hidden and the fragment corresponding to the menu item is shown. In this approach each fragment will not be created each time user switches menu. Thus pages will load faster.
I have this 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:checked="true"
android:icon="#drawable/ic_home_black_24dp"
android:title="Home" />
<item
android:id="#+id/nav_search"
android:icon="#drawable/ic_search_black"
android:title="Search Location" />
<item
android:id="#+id/nav_fav"
android:icon="#drawable/ic_favorite"
android:title="Favorites" />
<item
android:id="#+id/nav_recent"
android:icon="#drawable/ic_nav_route"
android:title="Recent Location" />
<item
android:id="#+id/nav_route"
android:icon="#drawable/ic_place"
android:title="Route" />
</group>
<item android:title="Others">
<menu>
<item
android:id="#+id/nav_settings"
android:icon="#drawable/ic_settings"
android:title="Settings" />
<item
android:id="#+id/nav_about"
android:icon="#android:drawable/ic_menu_send"
android:title="About" />
</menu>
</item>
</menu>
But when selecting the nav_settings,
the item is not check.
The item should be check just like the other items above.
This is my code on setting the item checked.
The fragment will just work fine.
but the selector on the nav_settings didn't work..
public void selectDrawerItem(MenuItem menuItem) {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
Fragment fragment = null;
Class fragmentClass;
switch(menuItem.getItemId()) {
case R.id.nav_home:
fragmentClass = FragmentMap.class;
setTitle("Map");
break;
case R.id.nav_search:
fragmentClass = FragmentSearchLoc.class;
Global.setCurrentItem =2;
break;
case R.id.nav_recent:
fragmentClass = FragmentSearchLoc.class;
Global.setCurrentItem =0;
break;
case R.id.nav_fav:
fragmentClass = FragmentSearchLoc.class;
Global.setCurrentItem =1;
break;
case R.id.nav_route:
fragmentClass = FragmentRoute.class;
setTitle("Routing");
break;
case R.id.nav_settings:
fragmentClass = FragmentSettings.class;
setTitle("Settings");
break;
default:
fragmentClass = FragmentMap.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
drawer
menuItem.setChecked(true);
drawer.closeDrawers();
}
I don't think nesting items like your code is allowed.
If you do like below, it may work:
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_settings"
android:icon="#drawable/ic_settings"
android:title="Settings" />
<item
android:id="#+id/nav_about"
android:icon="#android:drawable/ic_menu_send"
android:title="About" />
</group>