3

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.

  • Remove the padding attributes from the parent `RelativeLayout`, also set an orientation for your `LinearLayout` – Mark Aug 11 '16 at 15:41
  • No problem - Always pays to check the view hierarchy with issues like this – Mark Aug 11 '16 at 15:46
  • @MarkKeen see the edits. –  Aug 11 '16 at 15:55
  • I believe you should read this post on gravity. Otherwise, if you want to center things, use a RelativeLayout around the view you want centered. http://stackoverflow.com/questions/3482742/gravity-and-layout-gravity-on-android – OneCricketeer Aug 11 '16 at 16:04

3 Answers3

1

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>
Md Sufi Khan
  • 1,751
  • 1
  • 14
  • 19
  • Thank you! Now my button and the text box are at bottom of the gray box. Is there any way to align it in center? –  Aug 11 '16 at 15:44
  • set `android:gravity="center_horizontal"` or `android:gravity="center"` to your `LinearLayout` – Md Sufi Khan Aug 11 '16 at 15:45
  • I think it should be `center_vertical` . –  Aug 11 '16 at 15:47
  • You can set vertically, horizontally or both depending on your requirement. :) – Md Sufi Khan Aug 11 '16 at 15:48
  • Then you can try with `android:layout_gravity="center_vertical` on your text box and button – Md Sufi Khan Aug 11 '16 at 15:55
  • Solved problem my self: edited `android:paddingTop="25px"` and `android:paddingBottom="25px"` in linear layout. –  Aug 11 '16 at 16:12
  • Try remove any padding from `LinearLayout` and add `android:gravity="center_vertical"`. For my case its working fine. To see the output set a large height for `LinearLayout`. – Md Sufi Khan Aug 14 '16 at 12:44
0

Change the top part of your Relative layout ot this:

<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=".MainActivity"
    android:background="#eeeeee">

EDIT:

Change your LinearLayout like this:

<RelativeLayout          
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="50px"
    android:background="#666666">

    <LinearLayout          
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:orientation="horizontal">

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

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

    </LinearLayout>
</RelativeLayout>
Marat
  • 6,142
  • 6
  • 39
  • 67
  • Thank you! Now my button and the text box are at bottom of the gray box. Is there any way to align it in center? –  Aug 11 '16 at 15:45
  • @14bce109 I have updated my answer, take a look at it – Marat Aug 11 '16 at 15:51
  • not working. See the exact problem in my edited question –  Aug 11 '16 at 15:55
  • Did you set gravity to vertical? – Marat Aug 11 '16 at 16:00
  • @14bce109 try new solution given in answer. Replace your LinearLayout with the code given – Marat Aug 11 '16 at 16:06
  • The linear layout shifted left side instead of center –  Aug 11 '16 at 16:09
  • Solved problem my self: edited `android:paddingTop="25px"` and `android:paddingBottom="25px"` in linear layout –  Aug 11 '16 at 16:11
  • LinearLayout is set to match_parent. It can't shift anywhere in horizontal direction. What do you mean by that? – Marat Aug 11 '16 at 16:11
  • For future, avoid using `px` to specify the distance. Use `dp` instead. It will make sure that the paddings and margins are equal on all devices – Marat Aug 11 '16 at 16:13
0

In LinearLayout you are using android:paddingTop="50px". Instead use top and bottom padding 25px each.

Ramit
  • 416
  • 3
  • 8