I have a navigation menu in my HomeActivity extends AppCompatActivity
The drawer opens and closes and the menu items show up but when I click an item eg logout
The ids inside this code do not match what is in my menu xml
i.e.
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
Log.d(TAG, "getTitleCondensed is " +item.getTitleCondensed());
Log.d(TAG, "title is " +item.getTitle());
Log.d(TAG, "item.getItemId() is " + item.getItemId());
Log.d(TAG, "R.id.menu_logout " + R.id.menu_logout);
switch (item.getItemId()){
case R.id.menu_logout:
App.getInstance().setUserId(-1);
App.getInstance().setJwtToken(null);
Intent intent=new Intent(this,LoginActivity.class);
this.startActivity(intent);
return true;
}
return true;
}
return super.onOptionsItemSelected(item);
}
Logs show getTitleCondensed is null title is null item.getItemId() is 16908332 R.id.menu_logout 2131230987
The menu xml is as follows
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_questionaire"
android:title="Questionaire" />
<item
android:id="@+id/menu_dashboard"
android:title="Dashboard" />
<item
android:id="@+id/menu_logout"
android:title="Logout" />
</menu>
My Activity looks like this
public class DashboardActivity extends AppCompatActivity {
public DrawerLayout drawerLayout;
public ActionBarDrawerToggle actionBarDrawerToggle;
private static String TAG="DashboardActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
drawerLayout = findViewById(R.id.my_drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.nav_open, R.string.nav_close);
// pass the Open and Close toggle for the drawer layout listener
// to toggle the button
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
// to make the Navigation drawer icon always appear on the action bar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
Log.d(TAG, "getTitleCondensed is " +item.getTitleCondensed());
Log.d(TAG, "title is " +item.getTitle());
Log.d(TAG, "item.getItemId() is " + item.getItemId());
Log.d(TAG, "R.id.menu_logout " + R.id.menu_logout);
switch (item.getItemId()){
case R.id.menu_logout:
App.getInstance().setUserId(-1);
App.getInstance().setJwtToken(null);
Intent intent=new Intent(this,LoginActivity.class);
this.startActivity(intent);
return true;
}
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed () {
}
}
The layout is like this
<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/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DashboardActivity"
tools:ignore="HardcodedText">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="128dp"
android:gravity="center"
android:text="Dashboard"
android:textSize="18sp" />
</LinearLayout>
<!-- this the navigation view which draws and shows the navigation drawer -->
<!-- include the menu created in the menu folder -->
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="@+id/nav_menu_view"
app:menu="@menu/navigation_menu" />
</androidx.drawerlayout.widget.DrawerLayout>