BottomNavigationView text overlaps on icon - android

I have used BottomNavigationView in my app, but the text of menu item overlaps on menu icon in small devices as shown in below screenshot.
Edit:
Here is the java code:
public class MainActivity extends AppCompatActivity {
private static final String SELECTED_ITEM = "arg_selected_item";
private BottomNavigationView mBottomNav;
private int mSelectedItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBottomNav = (BottomNavigationView) findViewById(R.id.navigation);
BottomNavigationViewHelper.disableShiftMode(mBottomNav);
mBottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
selectFragment(item);
return true;
}
});
MenuItem selectedItem;
if (savedInstanceState != null) {
mSelectedItem = savedInstanceState.getInt(SELECTED_ITEM, 0);
selectedItem = mBottomNav.getMenu().findItem(mSelectedItem);
} else {
selectedItem = mBottomNav.getMenu().getItem(0);
}
selectFragment(selectedItem);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(SELECTED_ITEM, mSelectedItem);
super.onSaveInstanceState(outState);
}
#Override
public void onBackPressed() {
MenuItem homeItem = mBottomNav.getMenu().getItem(0);
if (mSelectedItem != homeItem.getItemId()) {
// select home item
selectFragment(homeItem);
} else {
super.onBackPressed();
}
}
private void selectFragment(MenuItem item) {
// init corresponding fragment
switch (item.getItemId()) {
case R.id.menu_explore:
pushFragment(new ExploreFragment());
break;
case R.id.menu_how_it_works:
pushFragment(new HowItWorksFragment());
break;
case R.id.menu_contact_us:
pushFragment(new ContactUsFragment());
break;
case R.id.menu_profile:
pushFragment(new MyProfileFragment());
break;
}
// update selected item
mSelectedItem = item.getItemId();
// uncheck the other items.
for (int i = 0; i < mBottomNav.getMenu().size(); i++) {
MenuItem menuItem = mBottomNav.getMenu().getItem(i);
menuItem.setChecked(menuItem.getItemId() == item.getItemId());
}
updateToolbarText(item.getTitle());
}
protected void pushFragment(Fragment fragment) {
if (fragment == null)
return;
FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (ft != null) {
ft.replace(R.id.container, fragment);
ft.commit();
}
}
}
private void updateToolbarText(CharSequence text) {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle(text);
}
}
}
Here is the xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:design="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="#android:color/white"
design:menu="#menu/bottom_nav_items" />
</LinearLayout>
How to resolve this issue ?
Please Help!!

Do not use long texts on bottom bar. It's out of guideline. Bottom Bar is not developed to use like that.
Text labels provide short, meaningfully definitions to bottom
navigation icons. Avoid long text labels as these labels do not
truncate or wrap.
Read all documentation and usage descriptions at this Source.

Related

PreferenceFragmentCompat inside (child of) Fragment not dispatching first touch event

i am creating setting of my app and am using fragments i made PreferenceFragmentCompat screen and in my parent Fragment i load it on setting click but when i first click on any preference its does not respond to my first touch but after some short time it works normal or when i scroll up or down fast and click on any preference right after it it does not respond to my touch event can someone help please?
public class SettingsParent extends Fragment {
Toolbar toolbar;
private HomeActivity homeActivity;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable final ViewGroup container, #Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
homeActivity = ((HomeActivity) getActivity());
View rootView = inflater.inflate(R.layout.settings_parent, container, false);
toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
setTitle(getString(R.string.settings));
setFragment(new SettingsFragment());
homeActivity.setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
homeActivity.onBackPressed();
}
});
rootView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Log.e("SettingTouch","parent");
return true;
}
});
android.support.v7.app.ActionBar actionBar = homeActivity.getSupportActionBar();
assert actionBar != null;
actionBar.setDisplayHomeAsUpEnabled(true);
return rootView;
}
private void setTitle(String string) {
toolbar.setTitle(string);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//inflater.inflate(R.menu.browser_options_menu, menu);
menu.setGroupVisible(R.id.grp, false);
super.onCreateOptionsMenu(menu, inflater);
}
void setFragment(Fragment newFragment){
FragmentManager fragmentManager = getChildFragmentManager();
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
android.support.v4.app.FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.replace(R.id.childF, newFragment, "detailFragment");
ft.commit();
}
}
this is xml of parentFragment.
<?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.support.design.widget.AppBarLayout
android:id="#+id/main.appbar"
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"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/child_fragment" />
</android.support.design.widget.CoordinatorLayout>
and this is my child PreferenceFragmentCompat.
public class SettingsFragment extends PreferenceFragmentCompat implements HomeActivity.Authentication, Preference.OnPreferenceClickListener, SharedPreferences.OnSharedPreferenceChangeListener, ConfirmAlertDialog.OnDialogClickInterface {
#Override
public boolean onPreferenceClick(final Preference preference) {
final String key = preference.getKey();
switch (key) {
case "remove_ads":
homeActivity.purchaseRemoveAds();
break;
case "hide_icon":
try {
homeActivity.loadFragment(new HideOptiosTutorial(), getParentFragment().getFragmentManager().beginTransaction());
} catch (Exception e) {
e.printStackTrace();
}
break;
case "key_share_app_link":
shareAppFile();
break;
case "move_to_sdcard":
showMovetoSdDialog();
break;
case "button_internal":
showMovetoInternalDialog();
break;
case "back_up_sdcard":
if (isSDCardUnmounted()) {
showSDCardMountError();
} else {
globalPasswords = getBackupPasswords();
if (!FileUtilis.isSafAllowed(getActivity())) {
showSDCardNotAllowedDialog(983);
//
return false;
}
if (globalPasswords.size() == 0) {
showNoBackupFoundMsg();
} else {
showDialogToBackupFromSDCard();
}
}
break;
case "change_password":
showChangePinDialog();
break;
case "key_rate_us":
rateApplication();
break;
case "app_link":
SavedAlbumsFragment.showAppLockLink(getActivity());
break;
case "view_intruders":
startActivity(new Intent(homeActivity, ViewIntruders.class));
break;
case "theme":
showSelectTehmeDialog();
break;
case "enter_email":
showChangeEmailDialog();
break;
}
return false;
}
#Override
public void onCreatePreferences(Bundle bundle, String s) {
addPreferencesFromResource(R.xml.pref_headers);
Log.e("Mateen","onCreatePreferences");
homeActivity = ((HomeActivity) getActivity());
homeActivity.addAuthenticationListner(this);
localDatabase = LocalDatabase.getInstance(getActivity());
localPreferences = new LocalPreferences(getActivity());
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
LocalBroadcastManager.getInstance(homeActivity).registerReceiver(broadcastReceiver,new IntentFilter("Remove_Is_Purchased"));
removeAdsPref=findPreference("remove_ads");
mCategory = (PreferenceCategory)findPreference("cat_remove_ads");
if (localPreferences.IsRemovedAdsPurchased())
{
mCategory.removePreference(removeAdsPref);
getPreferenceScreen().removePreference(mCategory);
}
else
{
removeAdsPref.setOnPreferenceClickListener(this);
}
findPreference("move_to_sdcard").setOnPreferenceClickListener(this);
findPreference("view_intruders").setOnPreferenceClickListener(this);
findPreference("button_internal").setOnPreferenceClickListener(this);
findPreference("back_up_sdcard").setOnPreferenceClickListener(this);
findPreference("change_password").setOnPreferenceClickListener(this);
findPreference("key_rate_us").setOnPreferenceClickListener(this);
findPreference("app_link").setOnPreferenceClickListener(this);
findPreference("hide_icon").setOnPreferenceClickListener(this);
findPreference("key_share_app_link").setOnPreferenceClickListener(this);
findPreference("listPref_wrong_tries").setOnPreferenceClickListener(this);
findPreference("theme").setOnPreferenceClickListener(this);
findPreference("enter_email").setOnPreferenceClickListener(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
findPreference("key_cam_short").setVisible(false);
findPreference("key_vid_short").setVisible(false);
}
if (!PreferenceManager.getDefaultSharedPreferences(homeActivity).getBoolean("key_intruder_detection",false))
{
findPreference("listPref_wrong_tries").setEnabled(false);
}
if (!Reprint.isHardwarePresent()) {
findPreference("key_fingerprint").setEnabled(false);
}
if (!FileUtilis.isSdcardPresent(getActivity())) {
findPreference("move_to_sdcard").setEnabled(false);
findPreference("button_internal").setEnabled(false);
findPreference("back_up_sdcard").setEnabled(false);
// findPreference("change_password").setEnabled(false);
findPreference("always_save_to_sdcard").setEnabled(false);
}
//add xml
}
but if i directly load child Fragment(SettingsParent) from my activity it works perfect without action bar.
can someone help or with better approach but i don't want to use activity i need fragment with action bar.
i solved my problem by removing
app:layout_scrollFlags="scroll|enterAlways"
from:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
now it is listening to my every damn touch :D
Happy coding

Blank activity is showing after destroying all fragments

I have created a main activity which has a fragment container in it, I am replacing many fragments in it on navigation menu option select. Its working fine, but the problem is:
When I click a navigation item twice, two identical fragments open, i have to press the back button twice to go back.
When all fragments are destroyed after pressing back, I see an empty page (which may be main activity page I guess).
layout_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/main_activity_page"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/pageBackgroundColor"
tools:context="co.sd.app.MainActivity">
<RelativeLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/side_nav_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:padding="0sp"
app:menu="#menu/side_navigation_menu" />
</android.support.v4.widget.DrawerLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Bundle bundle;
private SessionManager session;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarToggle;
private TextView cartItemCountDisplay;
private NavigationView sideNavView;
private Menu sideNavViewMenu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = (DrawerLayout) findViewById(R.id.main_activity_page);
actionBarToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.OPEN, R.string.CLOSE);
drawerLayout.addDrawerListener(actionBarToggle);
actionBarToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
bundle = new Bundle();
sideNavView = ((NavigationView) findViewById(R.id.side_nav_drawer));
sideNavViewMenu = sideNavView.getMenu();
//To Display Home Fragment On Page Load
displaySelectedItemResult(sideNavViewMenu.findItem(R.id.nav_home));
//To Adjust Login or Logout option in side navigation menu
if (session.isUserLoggedIn()) {
sideNavViewMenu.findItem(R.id.nav_login).setVisible(false);
sideNavViewMenu.findItem(R.id.nav_logout).setVisible(true);
} else {
sideNavViewMenu.findItem(R.id.nav_login).setVisible(true);
sideNavViewMenu.findItem(R.id.nav_logout).setVisible(false);
}
//Operation on side navigation item click
sideNavView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
displaySelectedItemResult(item);
//navDrawerLayout.closeDrawers();
return true;
}
});
}
private void displaySelectedItemResult(MenuItem item) {
sideNavViewMenu.findItem(R.id.nav_home).setChecked(false);
sideNavViewMenu.findItem(R.id.nav_contactUs).setChecked(false);
sideNavViewMenu.findItem(R.id.nav_aboutUs).setChecked(false);
sideNavViewMenu.findItem(R.id.nav_login).setChecked(false);
sideNavViewMenu.findItem(R.id.nav_logout).setChecked(false);
Fragment fragment;
switch (item.getItemId()) {
case R.id.nav_user:
if (session.isUserLoggedIn()) {
item.setTitle(session.getUserDetails().get(SessionManager.KEY_NAME));
} else {
item.setTitle("Login First!");
}
drawerLayout.closeDrawer(GravityCompat.START);
break;
case R.id.nav_home:
fragment = new HomeFragment();
if (fragment != null) {
callNavMenuItemsFragment(fragment, "FRAG_HOME");
}
item.setChecked(true);
drawerLayout.closeDrawer(GravityCompat.START);
break;
case R.id.nav_contactUs:
fragment = new ContactUsFragment();
if (fragment != null) {
callNavMenuItemsFragment(fragment, "FRAG_CONTACT_US");
}
item.setChecked(true);
drawerLayout.closeDrawer(GravityCompat.START);
break;
case R.id.nav_aboutUs:
fragment = new AboutUsFragment();
if (fragment != null) {
callNavMenuItemsFragment(fragment, "FRAG_ABOUT_US");
}
item.setChecked(true);
drawerLayout.closeDrawer(GravityCompat.START);
break;
case R.id.nav_login:
new UserAuthentication(MainActivity.this, session).loginAndSignupDialog();
sideNavViewMenu.findItem(R.id.nav_login).setVisible(false);
sideNavViewMenu.findItem(R.id.nav_logout).setVisible(true);
item.setChecked(true);
drawerLayout.closeDrawer(GravityCompat.START);
break;
case R.id.nav_logout:
session.logoutUser();
sideNavViewMenu.findItem(R.id.nav_login).setVisible(true);
sideNavViewMenu.findItem(R.id.nav_logout).setVisible(false);
item.setChecked(true);
drawerLayout.closeDrawer(GravityCompat.START);
break;
default:
}
}
private FragmentTransaction callNavMenuItemsFragment(final Fragment fragment, final String fragmentTag) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.addToBackStack(null);
//if (!ACTIVE_FRAGMENT_ID.equals(String.valueOf(fragment.getId()))) {
if (!activeFragmentIDsList.contains(String.valueOf(fragment.getId()))) {
ft.replace(R.id.fragment_container, fragment, fragmentTag);
activeFragmentIDsList.add(String.valueOf(fragment.getId()));
}
ft.commit();
return ft;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionBarToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
case R.id.cart:
if (session.isUserLoggedIn()) {
startActivity(new Intent(MainActivity.this, CartActivity.class));
} else if (new UserAuthentication(MainActivity.this, session).loginAndSignupDialog()) {
startActivity(new Intent(MainActivity.this, CartActivity.class));
}
break;
default:
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.actionbar_menu, menu);
final MenuItem menuItem = menu.findItem(R.id.cart);
MenuItemCompat.setActionView(menuItem, R.layout.cart_badge_layout);
final View actionView = MenuItemCompat.getActionView(menuItem);
cartItemCountDisplay = actionView.findViewById(R.id.cart_badge);
setupBadge();
actionView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setupBadge();
onOptionsItemSelected(menuItem);
}
});
return super.onCreateOptionsMenu(menu);
}
private void setupBadge() {
int itemCount = (((AppGlobalContent) getApplicationContext()).getSelectedItemsCount());
if (cartItemCountDisplay != null) {
if (itemCount == 0) {
if (cartItemCountDisplay.getVisibility() != View.GONE) {
cartItemCountDisplay.setVisibility(View.GONE);
}
} else {
cartItemCountDisplay.setText(String.valueOf(Math.min(itemCount, 99)));
if (cartItemCountDisplay.getVisibility() != View.VISIBLE) {
cartItemCountDisplay.setVisibility(View.VISIBLE);
}
}
}
}
#Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
Please suggest me on this.
This will fix your second problem, when you add fragments to your container they will be add to the stack,
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
finish();
}
else {
super.onBackPressed();
}
so when the stack entry count is 1 you have to finish the activity.
For the first problem you have to use "pop back stack" while adding the same fragment again use : getSupportFragmentManager().popBackStack();

BottomNavigationView keeps checking last menuitem (when it starts up) but displays first menuitem

When the app first starts up, it checks the last menuitem as if it was clicked. It displays the first menuitem content. In Android Studio, no errors, warnings, or logs are displayed that hint at anything going wrong.
Once the app is running, if I click on any of the other menuitems, it works normal. I can click on each of the menuitems and the correct fragment populates accordingly.
Here is MainActivity.java:
public class MainActivity extends AppCompatActivity {
private static final String SELECTED_ITEM = "arg_selected_item";
private BottomNavigationView mBottomNav;
private int mSelectedItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBottomNav = (BottomNavigationView) findViewById(R.id.navigation);
mBottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
selectFragment(item);
return true;
}
});
MenuItem selectedItem;
if (savedInstanceState != null) {
mSelectedItem = savedInstanceState.getInt(SELECTED_ITEM, 0);
selectedItem = mBottomNav.getMenu().findItem(mSelectedItem);
} else {
selectedItem = mBottomNav.getMenu().getItem(0);
}
selectFragment(selectedItem);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(SELECTED_ITEM, mSelectedItem);
super.onSaveInstanceState(outState);
}
#Override
/* This is never called on start up */
public void onBackPressed() {
MenuItem homeItem = mBottomNav.getMenu().getItem(0);
if (mSelectedItem != homeItem.getItemId()) {
// select home item
selectFragment(homeItem);
} else {
super.onBackPressed();
}
}
private void selectFragment(MenuItem item) {
Fragment frag = null;
// init corresponding fragment
switch (item.getItemId()) {
case R.id.menu_home:
frag = HomeFragment.newInstance();
break;
case R.id.menu_archives:
frag = ArchivesFragment.newInstance();
break;
case R.id.menu_inspirational:
frag = InspirationalFragment.newInstance();
break;
case R.id.menu_search:
frag = SearchFragment.newInstance();
break;
}
// update selected item
mSelectedItem = item.getItemId();
// unchecked the other items.
//THIS LINE HERE
for (int i = 0; i < mBottomNav.getMenu().size(); i++) {
MenuItem menuItem = mBottomNav.getMenu().getItem(i);
menuItem.setChecked(menuItem.getItemId() == mSelectedItem);
}
updateToolbarText(item.getTitle());
if (frag != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.container, frag, frag.getTag());
ft.commit();
}
}
private void updateToolbarText(CharSequence text) {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle(text);
}
}
Here is activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:design="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.ourdailystrength.mobile.android.MainActivity">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#f1f1f1">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
design:menu="#menu/bottom_nav_items" />
</LinearLayout>
Here is bottom_nav_items.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/menu_home"
android:title="#string/menu_home"
android:icon="#drawable/ic_home_black_48dp"
android:checked="true" <!-- Doesn't matter if this line is included or not -->
app:showAsAction="ifRoom"/>
<item
android:id="#+id/menu_archives"
android:title="#string/menu_archives"
android:icon="#drawable/ic_archive_black_48dp"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/menu_search"
android:title="#string/menu_search"
android:icon="#drawable/ic_search_black_48dp"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/menu_inspirational"
android:title="#string/menu_inspirational"
android:icon="#drawable/ic_photo_black_48dp"
app:showAsAction="ifRoom"/>
</menu>
Here is a screenshot of when it first boots up:
Why is the last menuitem being checked and not the first menuitem?
EDIT:
So, from answers given below, I made this change and it works:
private void selectFragment(MenuItem item) {
Fragment frag = null;
// update selected item
mSelectedItem = item.getItemId();
// unchecked the other items.
for (int i = 0; i < mBottomNav.getMenu().size(); i++) {
MenuItem menuItem = mBottomNav.getMenu().getItem(i);
menuItem.setChecked(false);
}
// init corresponding fragment
switch (mSelectedItem) {
case R.id.menu_home:
frag = HomeFragment.newInstance();
mBottomNav.getMenu().findItem(R.id.menu_home).setChecked(true);
break;
case R.id.menu_archives:
frag = ArchivesFragment.newInstance();
mBottomNav.getMenu().findItem(R.id.menu_archives).setChecked(true);
break;
case R.id.menu_inspirational:
frag = InspirationalFragment.newInstance();
mBottomNav.getMenu().findItem(R.id.menu_inspirational).setChecked(true);
break;
case R.id.menu_search:
frag = SearchFragment.newInstance();
mBottomNav.getMenu().findItem(R.id.menu_search).setChecked(true);
break;
}
updateToolbarText(item.getTitle());
if (frag != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.container, frag, frag.getTag());
ft.commit();
}
}
Why would the THIS LINE HERE in the above code cause it to do this weird error?
Hope this may help you to resolve your issue.
Call this method whenever you change the Fragment. Pass checkedcolor and unchecked color according to your needs.
private void changeMenuItemCheckedStateColor(int checkedColor, int uncheckedColor) {
int[][] states = new int[][]{
new int[]{-android.R.attr.state_checked}, // unchecked
new int[]{android.R.attr.state_checked}, // checked
};
int[] colors = new int[]{
uncheckedColor,
checkedColor
};
ColorStateList colorStateList = new ColorStateList(states, colors);
bottomNavigationView.setItemTextColor(colorStateList);
bottomNavigationView.setItemIconTintList(colorStateList);
}
There is 2 Solutions
1) Use Group
<group android:checkableBehavior="single">
<item .../>
<item .../>
<item .../>
</group>
2) Do it by programmatically
Menu bottomMenu;
BottomNavigationView bottomNavigationMenu = (BottomNavigationView) findViewById(R.id.bottom_navigation);
bottomMenu = navigation.getMenu();
bottomMenu.findItem(R.id.menu_home).setChecked(true);
Hope it will help , If you have any queries please comment.
Remove below code:
// unchecked the other items.
for (int i = 0; i < mBottomNav.getMenu().size(); i++) {
MenuItem menuItem = mBottomNav.getMenu().getItem(i);
menuItem.setChecked(menuItem.getItemId() == mSelectedItem);
}
and give tint color in BottomNavigationView:
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
app:itemIconTint="#drawable/nav_item_color_state"
design:menu="#menu/bottom_nav_items" />

Android BottomNavigationView menuItem set checked doesn't work properly

In my bottomNavigationView I can change programmaticaly my menu items everything it seems to work, but in fact my layout is diferent from if is checked or if I click directly on tab. Please look at differences bellow:
1) for menuItem.setChecked(true):
2) when a user clicks directly on icon it will perform the animation and show it on the correct
Well what I real want is to select the menuItem as if the user clicked. Isn't supposed to work by doing menuItem.setChecked(true)?
Please take into consideration the following definition layout for menu items:
<item android:id="#+id/menu_home"
android:title="#string/menu_home"
android:icon="#drawable/ic_home_black_24dp"
app:showAsAction="ifRoom" />
<item android:id="#+id/menu_hall_of_fame"
android:title="#string/menu_hall_of_fame"
android:icon="#drawable/ic_stars_black_24dp"
app:showAsAction="ifRoom" />
<item android:id="#+id/menu_info"
android:title="#string/menu_info"
android:icon="#drawable/ic_info_black_24dp"
app:showAsAction="ifRoom" />
<item android:id="#+id/menu_settings"
android:title="#string/menu_settings"
android:icon="#drawable/ic_settings_black_24dp"
app:showAsAction="ifRoom" />
and how I'm checking the correct menuItem:
MenuItem item = mBottomNav.getMenu().findItem(R.id.menu_about);
// update selected item
mSelectedItem = item.getItemId();
// uncheck the other items and select the one.
for (int i = 0; i< mBottomNav.getMenu().size(); i++) {
MenuItem menuItem = mBottomNav.getMenu().getItem(i);
menuItem.setChecked(menuItem.getItemId() == mSelectedItem);
}
There is another method to perform a selection of a menuItem? What am I missing here?
See This i post here all code
public class MainActivity extends AppCompatActivity {
private static final String SELECTED_ITEM = "arg_selected_item";
private BottomNavigationView mBottomNav;
private int mSelectedItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBottomNav = (BottomNavigationView) findViewById(R.id.navigation);
mBottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
selectFragment(item);
return true;
}
});
MenuItem selectedItem;
if (savedInstanceState != null) {
mSelectedItem = savedInstanceState.getInt(SELECTED_ITEM, 0);
selectedItem = mBottomNav.getMenu().findItem(mSelectedItem);
} else {
selectedItem = mBottomNav.getMenu().getItem(0);
}
selectFragment(selectedItem);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(SELECTED_ITEM, mSelectedItem);
super.onSaveInstanceState(outState);
}
#Override
public void onBackPressed() {
MenuItem homeItem = mBottomNav.getMenu().getItem(0);
if (mSelectedItem != homeItem.getItemId()) {
// select home item
selectFragment(homeItem);
} else {
super.onBackPressed();
}
}
private void selectFragment(MenuItem item) {
Fragment frag = null;
// init corresponding fragment
switch (item.getItemId()) {
case R.id.menu_home:
frag = MenuFragment.newInstance(getString(R.string.text_home),
getColorFromRes(R.color.color_home));
break;
case R.id.menu_notifications:
frag = MenuFragment.newInstance(getString(R.string.text_notifications),
getColorFromRes(R.color.color_notifications));
break;
case R.id.menu_search:
frag = MenuFragment.newInstance(getString(R.string.text_search),
getColorFromRes(R.color.color_search));
break;
}
// update selected item
mSelectedItem = item.getItemId();
// uncheck the other items.
Menu menu = mBottomNav.getMenu();
for (int i = 0, size = menu.size(); i < size; i++) {
MenuItem menuItem = menu.getItem(i);
((MenuItemImpl) menuItem).setExclusiveCheckable(false);
menuItem.setChecked(menuItem.getItemId() == R.id.menu_home);
((MenuItemImpl) menuItem).setExclusiveCheckable(true);
}
updateToolbarText(item.getTitle());
if (frag != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.container, frag, frag.getTag());
ft.commit();
}
}
private void updateToolbarText(CharSequence text) {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle(text);
}
}
private int getColorFromRes(#ColorRes int resId) {
return ContextCompat.getColor(this, resId);
}
}
you can use this code
Menu menu = mBottomNav.getMenu();
for (int i = 0, size = menu.size(); i < size; i++) {
MenuItem menuItem = menu.getItem(i);
((MenuItemImpl) menuItem).setExclusiveCheckable(false);
menuItem.setChecked(menuItem.getItemId() == R.id.menu_home);
((MenuItemImpl) menuItem).setExclusiveCheckable(true);
}

DrawerLayout not clickable after Fragment load

I followed the android guideline and completed a layout looks like this:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id= "#+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/fragmentContainer"/>
</LinearLayout>
<LinearLayout
android:id="#+id/left_drawer_view"
android:layout_width="250dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="start">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/wall"
android:layout_margin="0dp"
android:padding="0dp"
android:scaleType="center"
/>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/placeholder"
android:layout_margin="16dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="IGuessI'mUsername?"
android:textColor="#fff"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_margin="10dp"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:orientation="vertical"
android:background="#fff"
android:id="#+id/left_drawer_item"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/side_item_home"
android:text="Home"
android:drawablePadding="5dp"
android:padding="5dp"
android:gravity="center_vertical"
android:drawableLeft="#drawable/ic_menu_home"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Activities"
android:drawablePadding="5dp"
android:id="#+id/side_item_activities"
android:padding="5dp"
android:gravity="center_vertical"
android:drawableLeft="#drawable/ic_menu_search_holo_light"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Settings"
android:drawablePadding="5dp"
android:id="#+id/side_item_settings"
android:padding="5dp"
android:gravity="center_vertical"
android:drawableLeft="#drawable/ic_action_settings"
/>
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
FYI, I did not use Android.R.Id, but my own R.id.fragmentContainer to load the fragments. But apparently, the drawer layout is under the fragment loaded in the FrameLayout. If I click the item on the DrawerLayout, the fragments receive the gesture, not the DrawerLayout, although it still display correctly.
I need your two cents, guys. All answers and comments are appreciated.
Edit:
Here is the requested code.
public class ClipMe_main extends ActionBarActivity implements View.OnClickListener {
private HashMap<String, Stack<Fragment>> mStacks;
SlidingTabFragment homeFragment;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private String[] mTitle;
Fragment settingsFragment;
private ArrayList<DrawerItemModel> drawerData;
android.support.v7.app.ActionBar bar;
ActionBarDrawerToggle mDrawerToggle;
SlidingTabLayout mSlidingTabLayout;
public static DisplayMetrics displayMetrics;
FragmentManager fm;
FragmentPagerAdapter adapterViewPager;
int visiblePage;
TextView sideBarHome;
TextView sideBarActivities;
TextView sideBarSettings;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main_activity);
displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(displayMetrics);
registerReceiver(receiverDownloadComplete, new IntentFilter(DownloadService.ACTION_DOWNLOAD_COMPLETE));
sideBarHome = (TextView) findViewById(R.id.side_item_home);
sideBarActivities = (TextView) findViewById(R.id.side_item_activities);
sideBarSettings = (TextView) findViewById(R.id.side_item_settings);
sideBarSettings.setOnClickListener(this);
sideBarHome.setOnClickListener(this);
sideBarActivities.setOnClickListener(this);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
new Toolbar(ClipMe_main.this),
R.string.drawer_open,
R.string.drawer_close
) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(getTitle());
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle(getTitle());
}
};
setUpView();
setUpHomeFragment();
setUpSettingFragment();
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
toolbar.setBackgroundColor(getResources().getColor(R.color.swipe_refresh_2));
setSupportActionBar(toolbar);
mDrawerLayout.setDrawerListener(mDrawerToggle);
bar = this.getSupportActionBar();
bar.setDisplayHomeAsUpEnabled(true);
bar.setHomeButtonEnabled(true);
bar.setDisplayShowTitleEnabled(true);
}
private void setUpSettingFragment() {
settingsFragment = new SettingsFragment();
}
void setUpView() {
setContentView(R.layout.layout_main_activity);
}
void setUpHomeFragment() {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
homeFragment = new SlidingTabFragment();
transaction.add(R.id.fragmentContainer, homeFragment, "home");
transaction.commit();
}
public void pushFragments(String tag, Fragment fragment, boolean shouldAnimate) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
if (shouldAnimate)
ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
ft.replace(R.id.fragmentContainer, fragment, tag).addToBackStack(null);
ft.commit();
Log.d("FragmentManager", "Push " + tag);
}
public void getFragmentAndUpdate(int page) {
PopularFragment currentFragment = homeFragment.getCurrentFragment();
currentFragment.requestMoreData(page);
Log.d("FragmentPage", String.valueOf(page));
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_clipme_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
int id = item.getItemId();
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public int findVisibleFragment(){
Fragment homeFragment = (Fragment) getSupportFragmentManager().findFragmentByTag("home");
Fragment activitiesFragment = (Fragment) getSupportFragmentManager().findFragmentByTag("activities");
Fragment settingsFragment = (Fragment) getSupportFragmentManager().findFragmentByTag("settings");
Fragment likeFragment = (Fragment) getSupportFragmentManager().findFragmentByTag("like");
Fragment commentFragment = (Fragment) getSupportFragmentManager().findFragmentByTag("comment");
if(homeFragment!= null && homeFragment.isVisible()){
return 1;
} else if(activitiesFragment!= null && activitiesFragment.isVisible()){
return 2;
} else if (settingsFragment!= null && settingsFragment.isVisible()){
return 3;
} else if (likeFragment!= null && likeFragment.isVisible()){
return 4;
} else if (commentFragment!= null && commentFragment.isVisible()){
return 5;
} else
return 0;
}
#Override
public void onClick(View v) {
if (v == sideBarHome){
if (findVisibleFragment() == 1){
mDrawerLayout.closeDrawers();
} else {
mDrawerLayout.closeDrawers();
pushFragments("activities", homeFragment, true);
}
}
if (v == sideBarActivities){
if (findVisibleFragment() == 2){
mDrawerLayout.closeDrawers();
} else {
mDrawerLayout.closeDrawers();
pushFragments("activities", new ActivitiesFragment(), true);
}
}
if (v == sideBarSettings){
if (findVisibleFragment() == 1){
mDrawerLayout.closeDrawers();
} else {
mDrawerLayout.closeDrawers();
pushFragments("activities", settingsFragment, true);
}
}
}
}
The issue i found is in the onClick() method. Try changing the function as below
public Fragment findVisibleFragment() {
FragmentManager manager = getSupportFragmentManager();
Fragment fragment = manager.findFragmentById(R.id.fragmentContainer);
return fragment;
}
Edit:
set the same listener to multiple views.
sideBarHome.setOnClickListener(clickListener);
sideBarSettings.setOnClickListener(clickListener);
Refer this clicklistener object to the view and you can use the same logic to differentiate views
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
if (view.getId() == R.id.side_item_home) {
// mDrawerLayout.closeDrawers();
if (!(findVisibleFragment() instanceof SlidingTabFragment)) {
pushFragments("activities", homeFragment, true);
}
}
else if() {
//rest click logic
}
}
};

Categories

Resources