3

My problem is that I can't correctly set up the margin in my navigation drawer. I have tried changing margin of any view and item but it doesn't seem to work out. All I get is space between the ActionBar and NavigationDrawer (and the beginning of the page). I think that the problem is inside my ListItem layout but I am not sure. This is a photo of what I get

enter image description here

And this is my layout and code files.

Main Layout

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/drawer_layout">

<android.support.v4.widget.SwipeRefreshLayout

android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/refreshSites"
tools:context=".MyActivity">

<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sitesList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"/>
</android.support.v4.widget.SwipeRefreshLayout>

<RelativeLayout
    android:layout_width="280dp"
    android:layout_height="fill_parent"
    android:id="@+id/drawerPane"
    android:layout_gravity="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_marginTop="15dp">

<ListView android:id="@+id/left_drawer"
    android:layout_width="280dp"

    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/white"
    android:dividerHeight="0dp"
    android:layout_marginLeft="15dp"
    android:background="#FFF">
    </ListView>

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

drawer_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
    <TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:layout_marginTop="15dp"
    android:layout_marginStart="50dp"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#222"/>

My piece of code that works with NavigationDrawer

 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);   
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
        R.layout.drawer_list_item, mPlanetTitles)); //mPlanetTitles is String array

So the question is: How do I set these margins correctly?

1
  • 2
    Don't use margins, use padding instead, i think this will help.
    – Shivansh
    Apr 25 '15 at 19:58
2

Try this :

Using the help of Theme.add theme in DrawerLayout.

<style name="NavigationTheme" parent="AppTheme">
        <item name="android:layout_marginTop">5dp</item>
        <item name="android:layout_marginLeft">10dp</item>
</style>

Hope, this will help.

1

Try this as item

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:padding="20dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#222"
        android:text="bla bla bla" />

and main xml as this

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/drawer_layout">

<android.support.v4.widget.SwipeRefreshLayout

    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/refreshSites"
    tools:context=".MyActivity">

    <android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/sitesList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"/>
</android.support.v4.widget.SwipeRefreshLayout>

<RelativeLayout
    android:layout_width="280dp"
    android:layout_height="fill_parent"
    android:id="@+id/drawerPane"
    android:layout_gravity="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ListView android:id="@+id/left_drawer"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@android:color/white"
            android:dividerHeight="0dp"
            android:background="#FFF">
        </ListView>

    </LinearLayout>
</RelativeLayout>

Hope, this will help.

1
  • 1
    This is exactly what I was looking for. The problem was in using margin instead of padding. To be honest, it's a shame I didn't know the difference earlier but I hope now I will remember that :) Apr 25 '15 at 23:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.