0

I am using an AppCompatActivity and at the moment, I am trying to add a navigational drawer to the toolbar.

Problem is, now the cardview is not being displayed and the navigational drawer is also not working.

This is my code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="150dp">

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cards"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        card_view:cardCornerRadius="4dp">
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:padding="10dp"
            android:layout_alignParentBottom="true">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Username"
                android:id="@+id/usersName"
                android:layout_alignParentTop="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentEnd="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Others"
                android:id="@+id/others"
                android:layout_centerVertical="true"
                android:layout_alignParentStart="true" />

        </RelativeLayout>
    </android.support.v7.widget.CardView>
    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"></android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

Would really mean a lot and make my day if someone could fix my error please. I don't know where I have gone wrong, and I just want to have the navigation drawer button in the toolbar and the cardview on the screen

2 Answers2

0

Youre trying to squeeze a 200dp cardview inside a 150dp container. Use match_parent for drawerlayout. Then, straight from the developer guides:

The main content view must be the first child in the DrawerLayout because the XML order implies z-ordering and the drawer must be on top of the content.

The main content view is set to match the parent view's width and height, because it represents the entire UI when the navigation drawer is hidden.

For more information follow the developer resources

deefunkt
  • 331
  • 2
  • 12
0

Try this solution, this has android:layout_gravity="start" in NavigationView and CardView android:layout_height="match_parent" and some other minor changes.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="150dp">

    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cards"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        card_view:cardCornerRadius="4dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:padding="15dp">

            <TextView
                android:id="@+id/usersName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:text="Username"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:id="@+id/others"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="Others"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </RelativeLayout>
    </android.support.v7.widget.CardView>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:fitsSystemWindows="true">

    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142