How to implement OnLongClickListener to BottomNavigation menu item long press?

510 views Asked by At

I have issues with creating long press listener for my menu item.

I have tried using almost every solution on this side, but it didn't work. OnNavigationItemSelected works perfectly, but OnLongClickListener never fires whatever I do. When I declare MenuItem and find it by its ID, there is no function available that would do something OnLongClick.

What am I doing wrong?

navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/nav_home"
        android:icon="@drawable/home_navigation_button"
        />

    <item
        android:id="@+id/nav_leaderboard"
        android:icon="@drawable/leaderboard_navigation_button"
        />
    <item
        android:id="@+id/nav_addtree"
        android:icon="@drawable/addtree"
        />

    <item
        android:id="@+id/nav_search"
        android:icon="@drawable/search_navigation_button"
        />

    <item
        android:id="@+id/nav_profile"
        android:icon="@drawable/profile_navigation_button"
        />

</menu>

This is my activity layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginTest">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/navigation"
        />

</RelativeLayout>

This is my Activity

public class LoginTest extends AppCompatActivity {
    TextView userID;
    DataPresentersManager dataPresentersManager=new DataPresentersManager();
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_test);
        userID=(TextView)findViewById(R.id.userid);
        FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
        userID.setText(user.getUid());
        BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
        bottomNav.setOnNavigationItemSelectedListener(navListener);
        bottomNav.setItemIconTintList(null);
        //displayMainFragment();
    }


    private BottomNavigationView.OnNavigationItemSelectedListener navListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment selectedFragment =null;

                    switch(item.getItemId()){
                        case R.id.nav_home:
                                selectedFragment=dataPresentersManager.currentPresenter.getFragment();
                                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment);
                            break;
                        case R.id.nav_leaderboard:
                            break;
                        case R.id.nav_addtree:
                            break;
                        case R.id.nav_search:
                            break;
                        case R.id.nav_profile:
                            break;
                    }
                    return true;
                }
            };
}
1

There are 1 answers

0
Zain On

You can do that using bottomNav.findViewById() and pass in NavView menu item Id

View.OnLongClickListener listener = new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {

        switch (v.getId()) {
            case R.id.nav_home:
                break;
            case R.id.nav_leaderboard:
                break;
            case R.id.nav_addtree:
                break;
            case R.id.nav_search:
                break;
            case R.id.nav_profile:
                break;                  

        }
        return true;
    }
};

bottomNav.findViewById(R.id.nav_home).setOnLongClickListener(listener);
bottomNav.findViewById(R.id.nav_leaderboard).setOnLongClickListener(listener);
bottomNav.findViewById(R.id.nav_addtree).setOnLongClickListener(listener);
bottomNav.findViewById(R.id.nav_search).setOnLongClickListener(listener);
bottomNav.findViewById(R.id.nav_profile).setOnLongClickListener(listener);

My test: