user1445018
user1445018

Reputation: 157

navigation drawer does not fit in the activity

I am new to Android. I want to use a NavigationDrawer but it does not fit in the Activity. I am using ActionBarTab and Fragment in the Activity.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.basket.MainActivity" >
  <FrameLayout android:id="@+id/fragment_content"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:layout_weight="1" />

  <android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <ListView         
        android:padding="0dp" 
        android:id="@+id/left_drawer"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:background="#FFFFFF"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>

enter image description here

Upvotes: 0

Views: 63

Answers (2)

Jorge Casariego
Jorge Casariego

Reputation: 22212

Because you are new with this I recommend you follow these instructions to create a new Navigation Drawer

If you follow the recommendations you will see that here it is doing inversely that the way you're doing

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

Design

1. android.support.v4.widget.DrawerLayout 
  1.1 FrameLayout (Main content view)
  1.2 ListView (Navigation drawer)

With the image below you can understand better how it works

enter image description here

But if you want to continue doing like you show in your question you have to delete paddings attribute in your DrawerLayout like the example below

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.basket.MainActivity" >

    <FrameLayout android:id="@+id/fragment_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >
        <ListView
            android:padding="0dp"
            android:id="@+id/left_drawer"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:background="#FFFFFF"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />
    </android.support.v4.widget.DrawerLayout>

</RelativeLayout>

Upvotes: 1

Joseph Roque
Joseph Roque

Reputation: 5146

It is because of these lines in your xml:

android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"

They are adding an inner padding which prevents your drawer from reaching the edges of the fragment. Removing these lines will likely cause your other views to extend to the edges as well, so you'll have to adjust your layout as necessary.

Upvotes: 0

Related Questions