Muahmmad Tayyib
Muahmmad Tayyib

Reputation: 729

navigation drawer is always inflated when the app starts

I am trying to add navigation drawer to my main activity. in design view of activity_main.xml it should be visible at left side of the activity layout as a shadowed animation(i don't know what terminology i should use here for that :| ),which on drag/swipe to right should be visible. but in my case it is covering up the whole activity by default and is not displaying the actual contents of the activity ...which it ought to be...

i have gone through the solutionhere,but it is not working for me. my layout code for activity_main.xml is as following:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mts3_.p1_d3_app_bar.MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <include
        android:id="@+id/app_bar"
        layout="@layout/app_bar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/tv"
         />

</LinearLayout>


    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/drawer_menu"
        />


</android.support.v4.widget.DrawerLayout>

and drawer_menu.xml(layout file) is as following:

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">
        <item android:title="@string/img1"
            android:id="@+id/img11"
            android:icon="@drawable/pic_1"></item>
        <item android:title="@string/img2"
            android:id="@+id/img22"
            android:icon="@drawable/pic_2"></item>
        <item android:title="@string/img3"
            android:id="@+id/img33"
            android:icon="@drawable/pic_3"></item>
        <item android:title="@string/img4"
            android:id="@+id/img44"
            android:icon="@drawable/pic_4"></item>
    </group>
    <item android:title="@string/social">
        <menu>
            <item android:title="@string/add_to_group"
                android:id="@+id/add_to_group"
                android:icon="@drawable/pic_5"></item>
            <item android:title="@string/share"
                android:id="@+id/share"
                android:icon="@drawable/pic_6"></item>

            <item android:title="@string/group"
                android:id="@+id/group"
                android:icon="@drawable/pic_7"></item>

        </menu>


    </item>


</menu>

Upvotes: 1

Views: 1287

Answers (2)

KeLiuyue
KeLiuyue

Reputation: 8237

First,change your height and width android:layout_width="wrap_content" and android:layout_height="match_parent",

Second,set gravity android:layout_gravity="left" in your NavigationView.

Try this .

<android.support.design.widget.NavigationView  
    android:id="@+id/navigation_view"  
    android:layout_width="wrap_content"  
    android:layout_height="match_parent"  
    android:layout_gravity="left"   
    app:menu="@menu/drawer_menu"></android.support.design.widget.NavigationView>  

Edit

If you use android:layout_gravity="left" in your xml code , you will start NavigationView on the left . And android:layout_gravity="left" is the same of `android:layout_gravity="start``.

If you use android:layout_gravity="right" in your xml code , you will start NavigationView on the right.

And use tools:openDrawer="start" in your code ,it will display in the preview in the android studio .it did not effect your display in your device .

Upvotes: 1

Shivam Sharma
Shivam Sharma

Reputation: 420

Put below Code in Drawerlayout tag

 tools:openDrawer="start"

Like Following Code

<android.support.v4.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

After that implement that code in navigationView Tag

  android:layout_gravity="start"

Hope it work fine.

Upvotes: 0

Related Questions