Reputation: 303
I am aware that this question exists but the answers don't work for my case. I have a navigation drawer with four items, but when I click on the first one (the one i'm working on right now), the new activity I created doesn't open, I have looked and tried everything I found on the internet but nothing seems to work. i'm kinda new to android studio so I really don't know how to fix this. The android studio version I am using is 3.0. This is my menu xml:
<?xml version="1.0" encoding="utf-8"?>
<!--Icons made by http://www.freepik.com
from "https://www.flaticon.com
is licensed by http://creativecommons.org/licenses/by/3.0/-->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/m1" android:title="Agora Mall"
android:icon="@drawable/ic_ammunition"/>
<item android:id="@+id/m2" android:title="Downtown Center"
android:icon="@drawable/ic_ammunition"/>
<item android:id="@+id/m3" android:title="Galeria 360"
android:icon="@drawable/ic_ammunition"/>
<item android:id="@+id/m4" android:title="Sambil"
android:icon="@drawable/ic_ammunition"/>
</menu>
this is the main activity class code:
package com.example.arlet.storemaps;
import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.Window;
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item)) {
return true;
}
switch(item.getItemId()){
case R.id.m1:
Intent intent = new Intent(MainActivity.this, AgoraActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Upvotes: 1
Views: 759
Reputation: 1554
You must implement NavigationView.OnNavigationItemSelectedListener for the drawer items to work perfectly
Add this to your class declaration
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
// then overide this method to
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id){
case R.id.m1:
Intent intent = new Intent(MainActivity.this, AgoraActivity.class);
startActivity(intent);
return true;
//other cases goes here
}
return super.onOptionsItemSelected(item);
}
//you might want to ovewrite this method too
@Override
public void onBackPressed() {
if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
mDrawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
Upvotes: 0
Reputation: 293
You Can handle navigation drawer item click in following way:
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()) {
//Replacing the main content with ContentFragment Which is our Inbox View;
case R.id.m1:
Intent intent = new Intent(MainActivity.this, AgoraActivity.class);
startActivity(intent);
return true;
break;
case R.id.m2:
break;
case R.id.m3:
break;
case R.id.m4:
break;
}
menuItem.setChecked(true);
return true;
}
});
Upvotes: 0
Reputation:
You must implement onNavigationItemSelected()
for the navigation drawer menu.
onOptionsItemSelected()
is related to the toolbar options menu items.
You can find all the necessary code for this if you create a new project and choose
Navigation Drawer Activity
as the type of your MainActivity.
Upvotes: 1