2

As the title says, I have a BottomNavigationView with 3 Menu Items and want to assign a long click listener to each of them.
I have set an OnNavigationItemSelectedListener on the BottoNavigationView.

I have tried this answer here but that did not work out for me...

Any other way you guys recommend?

This is what I have...Regular clicks work btw

private void setUpNavigationListeners() {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.add(R.id.fragment_container, new CalendarView()).commit();
    navigation.setOnNavigationItemSelectedListener(
            new BottomNavigationView.OnNavigationItemSelectedListener() {

                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                    Fragment fragment;
                    switch (item.getItemId()) {
                        case R.id.navigation_courses:
                            subtitle.setText(R.string.courses);
                            fragment = new CalendarView();
                            break;
                        case R.id.navigation_assignments:
                            subtitle.setText(R.string.assignments);
                            fragment = new AssignmentView();
                            break;
                        case R.id.navigation_professors:
                            subtitle.setText(R.string.professors);
                            return false;
                        default:
                            return false;
                    }

                    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                    ft.replace(R.id.fragment_container, fragment).commit();
                    return true;
                }

            });
    navigation.setItemIconTintList(null);

    Menu menu = navigation.getMenu();
    MenuItem courses = menu.findItem(R.id.navigation_courses);
    MenuItem assignments = menu.findItem(R.id.navigation_assignments);
    MenuItem prof = menu.findItem(R.id.navigation_professors);

    courses.setActionView(new ImageButton(this));
    courses.getActionView().setLongClickable(true);
    Log.d("Manage", "Is long clickable:" + courses.getActionView().isLongClickable());
    courses.getActionView().setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Log.d("Manage", "Long clicked");
            return true;
        }
    });

}
Community
  • 1
  • 1
Pants
  • 2,563
  • 1
  • 20
  • 34

5 Answers5

1

There are two ways to solve the problem.

  1. By findViewById(int id);

    bottomNavigationView.findViewById(R.id.action_home).setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { return true; } });

  2. By 'menuView';

    private void overrideOnMenuItemLongClickListener(BottomNavigationView bottomNavigationView){
    
    if(bottomNavigationView == null){
        return;
    }
    
    int count = bottomNavigationView.getChildCount();
    if(count <= 0){
        return;
    }
    
    ViewGroup menuView = (ViewGroup) bottomNavigationView.getChildAt(0);
    if(menuView == null){
        return;
    }
    
    int menuItemViewSize = menuView.getChildCount();
    if(menuItemViewSize <= 0){
        return;
    }
    
    for(int i=0; i < menuItemViewSize; i++){
        menuView.getChildAt(i).setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });
    }
    

    }

Hai-Yang Li
  • 376
  • 2
  • 4
0

Here is the solution:

@Override
protected void onStart() {
    super.onStart();
    View view = findViewById(R.id.navigation_courses); // BottomNavigationView menu item id
    view.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            Toast.makeText(getApplicationContext(), "Long pressed", Toast.LENGTH_SHORT).show();
            return false;
        }
    });
}

In my case it works fine in onStart and doesn't in onCreate. If somebody understand why, please let me know.

GuessWho
  • 664
  • 1
  • 6
  • 19
0

Below JAVA Code absolutely works fine!!!

private void overrideOnMenuItemLongClickListener(BottomNavigationView bottomNavigationView) {
    if (bottomNavigationView != null && bottomNavigationView.getChildCount() > 0) {
        ViewGroup menuView = (ViewGroup) bottomNavigationView.getChildAt(0);
        if (menuView != null) {
            for (int i = 0; i < menuView.getChildCount(); i++) {
                menuView.getChildAt(i).setOnLongClickListener(v -> true);
            }
        }
    }
}
Srikanth P
  • 1,506
  • 14
  • 12
0
fun BottomNavigationView.setLonClkListner() {
    val position = 4 //item index in which u want to set LongClickListner
    val menuView = getChildAt(0) as ViewGroup
    menuView.getChildAt(position).setOnLongClickListener {
        findNavController().navigate(R.id.toSwitchAccount)
        true
    }
}
Abhijith mogaveera
  • 918
  • 10
  • 16
-1

Make setLongClickable(true); on each items in getView() so it becomes,

item.getActionView().setLongClickable(true);
item.getActionView().setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return false;
                }
            });
Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50
  • This did not work for me. I posted the snippet of code where I add the listeners. Thanks for your help. – Pants May 09 '17 at 14:30