4

I want to achieve something like this for my navigation drawer:

Enter image description here

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: https://stackoverflow.com/a/37336064/5816306

But what I am getting is something like this:

Enter image description here

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.

My XML code for drawer activity:

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

    <RelativeLayout
        android:id="@+id/holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:background="@color/colorAccent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <include
        layout="@layout/app_bar_main2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    </LinearLayout>
    </RelativeLayout>
    <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" />

</androidx.drawerlayout.widget.DrawerLayout>

This is navigation header XML:

<?xml version="1.0" encoding="utf-8"?>
<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">

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

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/nav_header_subtitle" />

</LinearLayout>

And my Main Activity code is:

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();
    }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Umair Iqbal
  • 1,959
  • 1
  • 19
  • 38
  • Set the `NavigationView`'s elevation to 0, set its background to transparent, and set your image as the background on the `DrawerLayout`. – Mike M. Apr 08 '20 at 09:23
  • I am still getting navigation view as separate with a line as in the above picture also I have removed header now how should I add the image – Umair Iqbal Apr 08 '20 at 09:47
  • I don't know what you mean by "separate". And you would set the image as the background on the `DrawerLayout` just like you would on any other `View`. – Mike M. Apr 08 '20 at 09:48
  • Wait, by "separate with a line", do you mean the shadow? If so, it looks like the androidx `DrawerLayout` now handles the drawer elevation itself internally, so you would set `app:elevation="0dp"` on the `` instead. – Mike M. Apr 08 '20 at 10:22
  • Iam updating my question with an image that's what Im getting I have set the elevation = 0dp in for draw layout and background image for drawer layout – Umair Iqbal Apr 08 '20 at 11:15
  • OK, I see what you mean about the "separate with a line" thing. That border around the `NavigationView`, yeah? If so, I've found that it's because the new `NavigationView` uses a `MaterialShapeDrawable` for its background, and it creates that from whatever color we set as the background in XML. You'll need to call `setBackground(null)` on the `NavigationView` in code to get rid of it. You can't do it in XML, because if it's `@null` there, the `MaterialShapeDrawable` is created anyway, with a default color, which seems to be solid black. – Mike M. Apr 08 '20 at 12:32
  • Exactly and I have got it by setting the background to null in code as you suggested. Can you please also tell me as you can see I have set the background to drawelayout but in the image that picture is only covering the navigation its not showing on whole screen – Umair Iqbal Apr 08 '20 at 12:52
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/211216/discussion-between-umair-iqbal-and-mike-m). – Umair Iqbal Apr 08 '20 at 12:55
  • Are you sure it's not the full width? That image looks like whatever part of the splash is on the right side is just being covered by the content `View`, on which you've set `colorAccent` as the background. I copied/pasted your layout for my test, substituting a random image I had, and it's working as expected. – Mike M. Apr 08 '20 at 12:57
  • Awesome man I just got it by removing background please can you suggest me any way to add image to top without adding header in navigation view.Like when I open drawer I can see profile picture on top And please post it as answer so I can accept it – Umair Iqbal Apr 08 '20 at 13:01
  • You should still be able to set a header layout, I would think; just make sure that its background is transparent, too. I'll test that to make sure, but I won't be able to do that, or to put together an answer, until later tonight. Glad we got it mostly working, at least. Cheers! – Mike M. Apr 08 '20 at 13:08

1 Answers1

1

I got the solution by just removing the background of Linearlayout in my XML code of Drawer Activity. I'm reposting the edited code:

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

    <RelativeLayout
        android:id="@+id/holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        **android:background="@color/colorAccent"**// Delete this line
        android:layout_height="match_parent"
        android:orientation="vertical">

    <include
        layout="@layout/app_bar_main2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    </LinearLayout>
    </RelativeLayout>
    <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" />

</androidx.drawerlayout.widget.DrawerLayout>

I have written in the code. Just delete that line of the code which is applying the background and you are good to go.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Umair Iqbal
  • 1,959
  • 1
  • 19
  • 38