14bce109 14bce109 - 3 months ago 15
Android Question

Linear Layout:How to fill the whole width of screen?

I've following layout file, which creates linear layout inside the relative layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#eeeeee">

<LinearLayout // I want this to fill the whole width of screen.
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="50px"
android:background="#666666"


>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/newEvent"
android:hint="Enter the Name of Event"
android:layout_below="@+id/textView2"
android:layout_alignParentStart="true" />


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Location"
android:id="@+id/pickerButton"
android:layout_alignTop="@+id/newEvent"
android:layout_toEndOf="@+id/newEvent" />



</LinearLayout>

<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Remainder"
android:id="@+id/submit"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View All"
android:id="@+id/view"
android:layout_below="@+id/submit"
android:layout_centerHorizontal="true"
android:layout_marginTop="68dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Location Will Appear Here"
android:id="@+id/textView4"

android:layout_above="@+id/submit"
android:layout_centerHorizontal="true"
android:layout_marginBottom="48dp" />

</RelativeLayout>


Using this code, I got following layout:

enter image description here

But There are spaces left in left, right and top of the gray color (linear-Layout). I don't want these space. I want to fill the whole width with gray color. What changes should I make in my
xml
to achieve this?

Thanks in advance!

EDIT:

Thanks for the answers. But after resolving this, I'm getting my text box and button at the bottom of the gray box, as shown in following figure:

enter image description here

For this, I've already tried
"android:gravity="center_horizontal"
,
"android:gravity="center_vertical"
and
"android:gravity="center"
. I've also tried
padding-bottom
.

But this is not working.

Answer

Remove

android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"

lines from parent RelativeLayout

<LinearLayout            
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="50px"
    android:background="#666666"  
    android:orientation="horizontal"
    android:gravity="center_vertical"
    >

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/newEvent"
        android:hint="Enter the Name of Event"
        android:layout_gravity="center_vertical" />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Location"
        android:id="@+id/pickerButton"
        android:layout_gravity="center_vertical" />



</LinearLayout>
Comments