material-components-android icon indicating copy to clipboard operation
material-components-android copied to clipboard

BottomNavigationView item's long click handling

Open wkns37 opened this issue 5 years ago • 3 comments

I want to handle long click on BottomNavigationView items for making own context menu like its in Instagram when user can hold "accout" icon and choose another profile. I use this code:

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

        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(listener);
        bottomNavigationView.setSelectedItemId(R.id.nav_messages);

        bottomNavigationView.findViewById(R.id.nav_profile).setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Toast.makeText(getApplicationContext(), R.string.dark_mode, Toast.LENGTH_SHORT).show();
                return true;
            }
        });

        showFragment(new MessagesFragment());
    }

But it works only when I start the app and holding item. If I start clicking on items and after that try to hold item again it does not work. It seems like listener stops working after user starts communicating with BottomNavigationView. Thank you.

wkns37 avatar Jun 03 '20 09:06 wkns37

We also have the same issue here.

In Android 8+ everything works fine, but in Android <8 it looks like the onLongClick() method is not called even if isLongClickable is true.

        (0 until count).forEach { index ->
            Log.d("★", "indexe=" + index)
            val menu = menuView?.getChildAt(index) as? BottomNavigationItemView ?: return@forEach
            Log.d("★", "menu.itemData.title=" + menu.itemData.title)
            if (menu.itemData.title == getString(R.string.account)) {
                menu.setIconTintList(null)
                Log.d("★", "isLongClickable=" + menu.isLongClickable)
                menu.setOnLongClickListener {
                    Log.d("★", "aaa")

log

2021-03-04 14:20:22.399 20344-20344/★: indexe=0
2021-03-04 14:20:22.399 20344-20344/★: menu.itemData.title=チャット
2021-03-04 14:20:22.399 20344-20344/★: indexe=1
2021-03-04 14:20:22.399 20344-20344/★: menu.itemData.title=タスク
2021-03-04 14:20:22.399 20344-20344/★: indexe=2
2021-03-04 14:20:22.399 20344-20344/★: menu.itemData.title=コンタクト
2021-03-04 14:20:22.399 20344-20344/★: indexe=3
2021-03-04 14:20:22.399 20344-20344/★: menu.itemData.title=アカウント
2021-03-04 14:20:22.399 20344-20344/★: isLongClickable=true

JeromeCHA avatar Mar 04 '21 06:03 JeromeCHA

@wkns37 @JeromeCHA Were you folks able to figure this out? I am facing the same issue where setOnLongClickListener on bottom bar is not triggering in any case. I just get isLongClickable's value false whenever I initialize the bottom bar.

dishankmehta avatar Jun 29 '24 14:06 dishankmehta

@dishankmehta Nah, it's been a while since the issue. I have switched to embedded C programming and ain't interested in Android development anymore. AFAIK, modern Jetpack Compose library has the ability to set both onClick and onLongClick listeners, but I'm not sure the library is ready to be in production.

wkns37 avatar Jul 13 '24 08:07 wkns37