material-components / material-components-android

Modular and customizable Material Design UI components for Android
Apache License 2.0
16.24k stars 3.05k forks source link

BottomNavigationView item's long click handling #1368

Open wkns37 opened 4 years ago

wkns37 commented 4 years ago

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.

JeromeCHA commented 3 years ago

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
dishankmehta commented 2 months ago

@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.

wkns37 commented 1 month ago

@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.