i am having a serious weird bug in android navigation view item click, the items are only clicking once when you open the app and stops clicking after, but the weirdest part is that the item is responding to click at the edge every time.
Below is my main xml code
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="ltr"
tools:openDrawer="start">
<include layout="@layout/layout_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:theme="@style/NavigationView"
app:itemTextColor="@color/black"
app:itemIconTint="@color/black"
android:background="@color/white"
app:headerLayout="@layout/nav_header_main"
android:elevation="5dp"
app:menu="@menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
Navigation menu items
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:checked="true"
android:id="@+id/home"
android:icon="@drawable/ic_home"
android:title="@string/home" />
<item
android:id="@+id/latest"
android:icon="@drawable/ic_latest"
android:title="@string/latest" />
<item
android:id="@+id/category"
android:icon="@drawable/ic_category"
android:title="@string/category" />
</group>
</menu>
The layout Frame
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/toolbar"
android:theme="@style/AppTheme.AppBarOverlay"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:layout_marginTop="55dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frame_one"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
My Activity code
package com.education.books.MySchoolLibrary;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;
public class NewAct extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
public Toolbar toolbar;
private ProgressBar progressBar;
private DrawerLayout drawer;
private NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Tool");
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
this.navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View headerview = navigationView.getHeaderView(0);
findViewById(R.id.frame_one).setOnClickListener(v -> {
Toast.makeText(this, "frame ti ya werey!!!", Toast.LENGTH_SHORT).show();
});
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
drawer.closeDrawers();
if (id == R.id.home) {
Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
} else if (id == R.id.latest) {
Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
} else if (id == R.id.category) {
Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
}
return true;
}
}
But if i copy this whole code into an entirely new application, it is working fine, pls i don't know what i am doing wrong. Thanks.
I am also facing same issue.found a work around solution like below
nav_view.menu.findItem(R.id.menu_share).setOnMenuItemClickListener {
shareApp()
drawer_layout.closeDrawer(GravityCompat.START)
true
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.