CHAPTER 6: Menu (Navigation Drawer Activity)
CHAPTER 6: Menu (Navigation Drawer Activity)
CHAPTER 6: Menu (Navigation Drawer Activity)
On the form factors screen, enable the Phone and Tablet option and set the
minimum SDK setting to API 26: Android 8.0 (Oreo). Continue through the
remaining screens, requesting the creation of a Navigation Drawer Activity named
NavDrawerActivity with a corresponding layout file named activity_nav_drawer.
Click on the Finish button to initiate the project creation process.
2
Option Menu Selected
Method Menu Option Selected in NavigationActivity.java
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Toast.makeText(getApplicationContext(), "Menu Setting
Selected ", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_menu_camera"
android:title="@string/menu_home" />
<item
android:id="@+id/nav_order"
android:icon="@drawable/ic_menu_send"
android:title="Ticket Order" />
</group>
</menu>
3
public class NavigationActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
NavigationView navigationView = findViewById(R.id.nav_view);
…………..
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_order) {
Intent i = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(i);
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Refrence :
https://www.techotopia.com/index.php/Implementing_an_Android_
Navigation_Drawer/