0

I am working on Bottom Navigation drawer notifications, when I click any item of Bottom Navigation for notification, it is showing icon on the wrong Item. Lets say if I am clicking Item5 and calling method to show Notification there, then it is showing Notification item on Item1. My code is given below, along with the snapshot of my screen. My fragment class is also given below:

MainActivity Class:

 AHBottomNavigation bottomNavigation;
    Fragment selectedFragment = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bottomNavigation = (AHBottomNavigation) findViewById(R.id.navigation);

        AHBottomNavigationItem item1 = new AHBottomNavigationItem(R.string.home,     R.drawable.home, R.color.colorAccent);
        AHBottomNavigationItem item2 = new AHBottomNavigationItem(R.string.menu,     R.drawable.menu, R.color.colorAccent);
        AHBottomNavigationItem item3 = new AHBottomNavigationItem(R.string.cart,     R.drawable.cart, R.color.colorAccent);
        AHBottomNavigationItem item4 = new AHBottomNavigationItem(R.string.orders,   R.drawable.orders, R.color.colorAccent);
        AHBottomNavigationItem item5 = new AHBottomNavigationItem(R.string.settings, R.drawable.setting, R.color.colorAccent);

        bottomNavigation.addItem(item1);
        bottomNavigation.addItem(item2);
        bottomNavigation.addItem(item3);
        bottomNavigation.addItem(item4);
        bottomNavigation.addItem(item5);

        bottomNavigation.setTitleState(AHBottomNavigation.TitleState.ALWAYS_SHOW);
        bottomNavigation.setAccentColor(Color.parseColor("#571e19"));

//        selectedFragment = ItemHomeFragment.newInstance(bottomNavigation);

        bottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
            @Override
            public boolean onTabSelected(int position, boolean wasSelected) {

                if (position == 0) {
                    selectedFragment = ItemHomeFragment.newInstance(bottomNavigation);
                } else if (position == 1) {
                    selectedFragment = ItemMenuFragment.newInstance(bottomNavigation);
                } else if (position == 2) {
                    selectedFragment = ItemCardFragment.newInstance(bottomNavigation);
                } else if (position == 3) {
                    selectedFragment = ItemOrdersFragment.newInstance(bottomNavigation);
                } else if (position == 4) {
                    selectedFragment = ItemSettingsFragment.newInstance(bottomNavigation);
                }

                android.app.FragmentManager fragmentManager = getFragmentManager();
                android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.frame_layout,selectedFragment);
                fragmentTransaction.commit();


                return true;

            }

        });

    }

    @Override
    protected void onStart() {
        super.onStart();
        android.app.FragmentManager fragmentManager = getFragmentManager();
        android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.frame_layout, ItemHomeFragment.newInstance(bottomNavigation));
        fragmentTransaction.commit();

    }

My Fragment Class:

public class ItemSettingsFragment extends Fragment {

    public static AHBottomNavigation bottomNavigation1;

    public static ItemSettingsFragment newInstance(AHBottomNavigation bottomNavigation) {
        ItemSettingsFragment fragment = new ItemSettingsFragment();
        // Initializing Navigation Drawer
        bottomNavigation1 = bottomNavigation;
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        View view = inflater.inflate(R.layout.fragment_settings, container, false);

        setNotification();
        return view;

    }


    // Calling this method in my 5 Item of Notification Drawer, but '1' is showing in first item of Bottom Noficiation drawer
    public static void setNotification(){
        bottomNavigation1.setNotification("1", 1);
    }

}

Clicking at Item5 but notification icon is appearing at Item2 of my Notification Drawer items.

enter image description here

Usman Khan
  • 3,739
  • 6
  • 41
  • 89

2 Answers2

1

I have checked your code throughout and tried to compare it with github repo for AHBottomNavigation and guess what? I found that small mistake.

public static void setNotification(){
    bottomNavigation1.setNotification("1", 1);
}

Now check this line, first parameter is your notification counter and second parameter is your tab number, you are passing '1' which means set notification badge on '1' tab, and because tabs counter start with zero that's why it is setting on the second tab.

The answer is simple. Just change it with this

public static void setNotification(){
    bottomNavigation1.setNotification("1", 4);
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Najam
  • 1,129
  • 11
  • 32
  • thanks for your response. I was doing a silly mistake. Thanks alot it works. – Usman Khan Nov 05 '17 at 07:01
  • I recommend you guys check my solution, you might have missed it, as it was posted before this one. Najam, changing the value is not the best solution, I proposed a way to modify the method, by adding arguments, allowing to use variables when calling the method. This offers more flexibility. U Khan, when you will need to increment or remove notifications, you will need a design like the one I proposed. Hope it helps. – KJ Newtown Nov 05 '17 at 14:46
0

When you do:

   public static void setNotification(){
    bottomNavigation1.setNotification("1", 1);
}

"1" is the notification

1 is the item postion

So you are setting the notification at the item at postion 1: "Menu"

What you could do:

Modify the method this way:

   public static void setNotification(String notification, int itemPostion){
bottomNavigation1.setNotification(motification, itemPostion);

}

Then, when you call the method:

setNotification("Any message or number", yourItemPostion)

example: set "2"on Settings:

setNotification("2", 4)
KJ Newtown
  • 328
  • 1
  • 16