Monday, September 20, 2021

Steps to create Menu on Activity Android (Navigation Drawer)


 Adding Menu on your Activity in Android can be a simple three step process:

Step 1: Create your Menu Resource Files

Step 2: Create your android activity’s layout resource file. drawerlayout.widget should be your parent element for your menu resources to be rendered on the app.

Step 3: Create your Java Class file and activate your menu widget programmatically,

 Here is the code:

Step 1: Start out by creating two menu resource file under menu tab:

First File:  menu_main.xml

 <menu 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"
   
tools:context="com.example.epbr.Second_Activity">
    <
item
       
android:id="@+id/action_settings"
       
android:orderInCategory="100"
       
android:title="@string/action_settings"
       
app:showAsAction="never" />
</
menu>

 

SeSecond File: navigation_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:ignore="HardcodedText"
>
    <item
       
android:id="@+id/nav_account"
      
android:title="My Account"
/>
    <item
       
android:id="@+id/nav_custom"
       
android:title="My Contribution"
       
/>
    <item
      
android:id="@+id/nav_settings"
      
android:title="Settings"
/>
    <item
       
android:id="@+id/nav_logout"
       
android:title="Logout"
/>
</menu>
String Resources: Set Open/Close navigation drawer in String Resource File. 
Add the following code to string.xml:
<!--to toggle the open close button of the navigation drawer-->
<string name="nav_open">Open</string>
<string name="nav_close">Close</string>
 
Step 2: Adding menu on your android activity
 
<?xml version="1.0" encoding="utf-8"?>
<!--the root view must be the DrawerLayout-->
<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=".Second_Activity"
   
tools:ignore="HardcodedText"
>
    <com.google.android.material.navigation.NavigationView
       
android:layout_width="wrap_content"
       
android:layout_height="match_parent"
       
android:layout_gravity="start"
      
app:menu="@menu/navigation_menu"
/>

    <LinearLayout 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:layout_width="match_parent"
       
android:layout_height="match_parent"
       
android:orientation="vertical"
       
tools:context=".YOUR_Activity"
>
        //YOUR LAYOUT ELEMENTS WILL GO HERE
    </LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>

Step 3: Activate the Menu elements on Java Class file.

public class YOUR_Activity extends AppCompatActivity {
    public DrawerLayout drawerLayout;
    public ActionBarDrawerToggle actionBarDrawerToggle;
     @Override
   
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_activity);
        if(savedInstanceState == null) {
               // If you are using fragments you can call them here
         }
        // drawer layout instance to toggle the menu icon to open
        
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
       
drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();
        // to make the Navigation drawer icon always appear on the action bar
       
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
 
// drawer when the icon is clicked
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}
 
 

No comments: