CHAPTER 6: Menu (Navigation Drawer Activity)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

CHAPTER 6: Menu (Navigation Drawer Activity)

Creating the Navigation Drawer Template Project


Create new Activity in menu
File→New →Activity → Navigation Drawer Activity

Figure 1 . New 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.

Attributes of Android RadioButton


Once the project has been created, it will contain the following XML resource files
located under app -> res -> layout in the Project tool window:

• activity_nav_drawer.xml – This is the top level layout resource file. It


contains the DrawerLayout container and the NavigationView child. The
NavigationView declaration in this file indicates that the layout for the drawer
header is contained within the nav_header_nav_drawer.xml file and that the
1
menu options for the drawer are located in the
activity_nav_drawer_drawer.xml file. In addition, it includes a reference to
the app_bar_nav_drawer.xml file.
• app_bar_nav_drawer.xml – This layout resource file is included by the
activity_nav_drawer.xml file and is the standard app bar layout file built
within a CoordinatorLayout container as covered in the preceding chapters. As
with previous examples this file also contains a directive to include the content
file which, in this case, is named content_nav_drawer.xml.
• content_nav_drawer.xml – The standard layout for the content area of the
activity layout. This layout consists of a ConstraintLayout container and a
“Hello World!” TextView.
• nav_header_nav_drawer.xml – Referenced by the NavigationView element
in the activity_nav_drawer.xml file this is a placeholder header layout for the
drawer.

The Header Coloring Resource File


In addition to the layout resource files, the side_nav_bar.xml file located under app
→> res→> drawable may be modified to change the colors applied to the drawer
header. By default, this file declares a rectangular color gradient transitioning
horizontally from dark to light green.
The Template Menu Resource File
The menu options presented within the navigation drawer can be found in the
activity-nav-drawer-drawer.xml file located under app → res → menu in the project
tool window. By default, the menu consists of a range of text based titles with
accompanying icons (the files for which are all located in the drawable folder). For
more details on menu resource files, refer to the chapter entitled Creating and
Managing Overflow Menus on Android.
Add/Edit Menu Option
The menu options presented within the navigation drawer can be found in the
navigation.xml file located under app → res → menu.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
</menu>

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);
}

Add/Edit Menu Navigation


The menu options presented within the navigation drawer can be found in the
activity-nav-drawer-drawer.xml file located under app → res → menu.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">

<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>

Navigation Menu Selected


Method Menu Navigation Selected in NavigationActivity.java

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;
}

Running the App


Compile and run the project and note the appearance of the drawer indicator as
highlighted in Figure 2

Refrence :
https://www.techotopia.com/index.php/Implementing_an_Android_
Navigation_Drawer/

You might also like