title: Custom navigation drawer on Android
body: <p>I want to achieve something like this for my navigation drawer:</p>
  
  <p><a href="https://i.stack.imgur.com/2geGV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2geGV.png" alt="Enter image description here"></a></p>
  
  <p>I followed this answer from Stack Overflow and almost got the thing except the view is not viewing properly. This is the answer I followed: <a href="https://stackoverflow.com/a/37336064/5816306">https://stackoverflow.com/a/37336064/5816306</a></p>
  
  <p>But what I am getting is something like this:</p>
  
  <p><a href="https://i.stack.imgur.com/g3ePX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/g3ePX.png" alt="Enter image description here"></a></p>
  
  <p>As you can see, in my view the navigation drawer is coming separately. It not getting the whole window and the view is separate. I want to show it as the first image like the whole screen should have only one image and a small part of the main screen should be shown like in the first image.</p>
  
  <p><strong>My XML code for drawer activity:</strong></p>
  
  <pre><code>  &lt;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/drawer_layout"
      app:elevation="0dp"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@drawable/splash"
      android:fitsSystemWindows="true"
      tools:openDrawer="start"&gt;
  
      &lt;RelativeLayout
          android:id="@+id/holder"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"&gt;
  
      &lt;LinearLayout
          android:id="@+id/content"
          android:layout_width="match_parent"
          android:background="@color/colorAccent"
          android:layout_height="match_parent"
          android:orientation="vertical"&gt;
  
      &lt;include
          layout="@layout/app_bar_main2"
          android:layout_width="match_parent"
          android:layout_height="match_parent" /&gt;
      &lt;/LinearLayout&gt;
      &lt;/RelativeLayout&gt;
      &lt;com.google.android.material.navigation.NavigationView
          android:id="@+id/nav_view"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          android:background="@android:color/transparent"
          app:menu="@menu/activity_main2_drawer" /&gt;
  
  &lt;/androidx.drawerlayout.widget.DrawerLayout&gt;
  </code></pre>
  
  <p><strong>This is navigation header XML:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="@dimen/nav_header_height"
      android:background="@drawable/side_nav_bar"
      android:gravity="bottom"
      android:orientation="vertical"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:theme="@style/ThemeOverlay.AppCompat.Dark"&gt;
  
      &lt;ImageView
          android:id="@+id/imageView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:contentDescription="@string/nav_header_desc"
          android:paddingTop="@dimen/nav_header_vertical_spacing"
          app:srcCompat="@mipmap/ic_launcher_round" /&gt;
  
      &lt;TextView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:paddingTop="@dimen/nav_header_vertical_spacing"
          android:text="@string/nav_header_title"
          android:textAppearance="@style/TextAppearance.AppCompat.Body1" /&gt;
  
      &lt;TextView
          android:id="@+id/textView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/nav_header_subtitle" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>And my Main Activity code is:</strong></p>
  
  <pre><code>private AppBarConfiguration mAppBarConfiguration;
      View contentView;
      DrawerLayout drawerLayout;
      private static final float END_SCALE = 0.7f;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main2);
          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 = findViewById(R.id.drawer_layout);
          contentView = findViewById(R.id.content);
          NavigationView navigationView = findViewById(R.id.nav_view);
          // Passing each menu ID as a set of Ids because each
          // menu should be considered as top level destinations.
          mAppBarConfiguration = new AppBarConfiguration.Builder(
                  R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
                  .setDrawerLayout(drawerLayout)
                  .build();
          NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
          NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
          NavigationUI.setupWithNavController(navigationView, navController);
  
          drawerLayout.setScrimColor(Color.TRANSPARENT);
          drawerLayout.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
                                             @Override
                                             public void onDrawerSlide(View drawerView, float slideOffset) {
  
                                                 // Scale the View based on current slide offset
                                                 final float diffScaledOffset = slideOffset * (1 - END_SCALE);
                                                 final float offsetScale = 1 - diffScaledOffset;
                                                 contentView.setScaleX(offsetScale);
                                                 contentView.setScaleY(offsetScale);
  
                                                 // Translate the View, accounting for the scaled width
                                                 final float xOffset = drawerView.getWidth() * slideOffset;
                                                 final float xOffsetDiff = contentView.getWidth() * diffScaledOffset / 2;
                                                 final float xTranslation = xOffset - xOffsetDiff;
                                                 contentView.setTranslationX(xTranslation);
                                             }
  
                                             @Override
                                             public void onDrawerClosed(View drawerView) {
                                             }
                                         }
          );
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main2, menu);
          return true;
      }
  
      @Override
      public boolean onSupportNavigateUp() {
          NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
          return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                  || super.onSupportNavigateUp();
      }
  </code></pre>