Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BottomNavigationView item's long click handling #1368

Open
wkns37 opened this issue Jun 3, 2020 · 3 comments
Open

BottomNavigationView item's long click handling #1368

wkns37 opened this issue Jun 3, 2020 · 3 comments
Labels
bug Good First Issue Issues that first time contributors can work on Widget: BottomNavigation

Comments

@wkns37
Copy link

wkns37 commented Jun 3, 2020

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
Copy link

JeromeCHA commented Mar 4, 2021

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

@drchen drchen added the Good First Issue Issues that first time contributors can work on label May 4, 2022
@dishankmehta
Copy link

@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
Copy link
Author

wkns37 commented Jul 13, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Good First Issue Issues that first time contributors can work on Widget: BottomNavigation
Projects
None yet
Development

No branches or pull requests

4 participants