0
A main Activity:
package com.example.italo.nvigationdrawer;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener {
private Toolbar toolbar;
private DrawerLayout drawerLayout;
private NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = findViewById(R.id.drawerLayout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout,toolbar,R.string.open_drawer,R.string.close_drawer);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
navigationView = findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}else {
super.onBackPressed();
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()){
case R.id.menu_inbox:{
Toast.makeText(this,"Menu Inbox" , Toast.LENGTH_SHORT).show();
break;
}
case R.id.menu_starred:{
Toast.makeText(this,"Menu Favoritos", Toast.LENGTH_SHORT).show();
break;
}
case R.id.menu_sent_email:{
Toast.makeText(this, "Menu Enviados", Toast.LENGTH_SHORT).show();
break;
}
case R.id.menu_trash:{
Toast.makeText(this, "Menu Lixeira", Toast.LENGTH_SHORT).show();
break;
}
case R.id.menu_spam:{
Toast.makeText(this, "Menu Spam", Toast.LENGTH_SHORT).show();
break;
}
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
}
XML OF ACTIVITY
<?xml version="1.0" encoding="utf-8"?>
android:id="@+id/drawerLayout"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
app:menu="@menu/menu_nav"
/>
<include layout="@layout/toolbar"/>
The Menu Class itself:
<?xml version="1.0" encoding="utf-8"?>
<group android:checkableBehavior="single">
<item android:title="@string/inbox"
android:id="@+id/menu_inbox"
android:icon="@drawable/ic_inbox"/>
<item android:title="@string/favoritos"
android:id="@+id/menu_starred"
android:icon="@drawable/ic_starred"/>
<item android:title="@string/emails_enviados"
android:id="@+id/menu_sent_email"
android:icon="@drawable/ic_sent_email"/>
</group>
<item android:title="@string/outros"
android:id="@+id/submenu_outros">
<menu>
<item android:title="@string/lixeira"
android:id="@+id/menu_trash"
android:icon="@drawable/ic_trash"/>
<item android:title="@string/spam"
android:id="@+id/menu_spam"
android:icon="@drawable/ic_spam"/>
</menu>
</item>
Post your Activity’s xml code
– Leandro Araujo
All right, you’re up there
– Italo Silva