How to create a custom navigation drawer in Android?

This example demonstrate about How to resize Image in Android App.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

 xml version= "1.0" encoding= "utf-8" ?>

   
   

Step 3 − Add the following code to res/layout/nav_header_main.xml.

 xml version= "1.0" encoding= "utf-8" ?>

   
   
   

Step 4 − Add the following code to res/layout/app_bar_main.xml.

 xml version= "1.0" encoding= "utf-8" ?>

   
   
   
   
   

Step 5 − Add the following code to res/layout/content_main.xml.

 xml version= "1.0" encoding= "utf-8" ?>

Step 6 − Add the following code to res/menu/activity_main_drawer.xml.

 xml version= "1.0" encoding= "utf-8" ?>

   
      
      
      
      
   
   
      
         
         
      
   

Step 7 − Add the following code to src/MainActivity.java

package app.tutorialspoint.com.sample ;
import android.os.Bundle ;
import android.support.annotation. NonNull ;
import android.support.design.widget.FloatingActionButton ;
import android.support.design.widget.Snackbar ;
import android.view.View ;
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.support.v7.widget.Toolbar ;
import android.view.Menu ;
import android.view.MenuItem ;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
   @Override
   protected void onCreate (Bundle savedInstanceState) {
      super .onCreate(savedInstanceState) ;
      setContentView(R.layout. activity_main ) ;
      Toolbar toolbar = findViewById(R.id. toolbar ) ;
      setSupportActionBar(toolbar) ;
      FloatingActionButton fab = findViewById(R.id. fab ) ;
      fab.setOnClickListener( new View.OnClickListener() {
         @Override
         public void onClick (View view) {
            Snackbar. make (view , "Replace with your own action" ,
            Snackbar. LENGTH_LONG )
            .setAction( "Action" , null ).show() ;
         }
      }) ;
      DrawerLayout drawer = findViewById(R.id. drawer_layout ) ;
      ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer , toolbar , R.string. navigation_drawer_open ,
      R.string. navigation_drawer_close ) ;
      drawer.addDrawerListener(toggle) ;
      toggle.syncState() ;
      NavigationView navigationView = findViewById(R.id. nav_view ) ;
      navigationView.setNavigationItemSelectedListener( this ) ;
   }
   @Override
   public void onBackPressed () {
      DrawerLayout drawer = findViewById(R.id. drawer_layout ) ;
      if (drawer.isDrawerOpen(GravityCompat. START )) {
         drawer.closeDrawer(GravityCompat. START ) ;
      } else {
         super .onBackPressed() ;
      }
   }
   @Override
   public boolean onCreateOptionsMenu (Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu. main , menu) ;
      return true;
   }
   @Override
   public boolean onOptionsItemSelected (MenuItem item) {
      int id = item.getItemId() ;
      if (id == R.id. action_settings ) {
         return true;
      }
      return super .onOptionsItemSelected(item) ;
   }
   @SuppressWarnings ( "StatementWithEmptyBody" )
   @Override
   public boolean onNavigationItemSelected ( @NonNull MenuItem item) {
      // Handle navigation view item clicks here.
      int id = item.getItemId() ;
      if (id == R.id. nav_camera ) {
         // Handle the camera action
      } else if (id == R.id. nav_gallery ) {
      } else if (id == R.id. nav_slideshow ) {
         } else if (id == R.id. nav_manage ) {
         } else if (id == R.id. nav_share ) {
         } else if (id == R.id. nav_send ) {
      }
      DrawerLayout drawer = findViewById(R.id. drawer_layout ) ;
      drawer.closeDrawer(GravityCompat. START ) ;
      return true;
   }
}

Step 8 − Add the following code to androidManifest.xml

 xml version= "1.0" encoding= "utf-8" ?>

   
      
         
            
            
         
      
   

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen –

Updated on: 2019-07-30T22:30:26+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements