Pooja Dubey
Pooja Dubey

Reputation: 683

Set button below a button in a Linear Layout

I have to create a layout that have two buttons in that .Second button will be below the first one .See in this XML i have done that but i am not able to set the postion of the button below the button1.Please help me to get this .

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="2dp"
        android:orientation="horizontal"
        android:weightSum="3" >

        <TextView
            android:id="@+id/ap"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:singleLine="true"
            android:text="TEST" />

        <TextView
            android:id="@+id/st"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:singleLine="true"
            android:text="TEST" />

        <TextView
            android:id="@+id/tt"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:singleLine="true"
            android:text="TEST" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="36dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:text="Button" />
    </LinearLayout>

</RelativeLayout>

Upvotes: 0

Views: 8387

Answers (8)

user5380273
user5380273

Reputation:

With the first line it will be bellow your first button and with the seccond line, you say how big the gap between the two buttons is.

android:layout_below="@+id/button"
android:layout_marginTop="5dp"

Upvotes: 0

Luciano
Luciano

Reputation: 2798

You can remove the android:weightSum in the LinearLayout, and remove the layout_weight in all the views inside the LinearLayout.

Because you have set a weighSum of 3, the linearLayout will only show the first 3 items in it because they have a total weight of 3.

Upvotes: 0

gior91
gior91

Reputation: 1775

Create a LinearLayout with Vertical orientation inside the parent linearLayout2 in this way:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="2dp"
    android:orientation="horizontal"
    android:weightSum="3" >

    <TextView
        android:id="@+id/ap"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:singleLine="true"
        android:text="TEST" />

    <TextView
        android:id="@+id/st"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:singleLine="true"
        android:text="TEST" />

    <TextView
        android:id="@+id/tt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:singleLine="true"
        android:text="TEST" />


    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="2dp"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="36dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:text="Button" />
       <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="36dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:text="Button" />
    </LinearLayout>
</LinearLayout>

Adjust margins, weights and dimensions as you want.

Upvotes: 2

Amresh
Amresh

Reputation: 2108

Add the second button after linearLayout2.

<Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/linearLayout2"
        android:text="Button" />

Upvotes: 0

Abhishek Agarwal
Abhishek Agarwal

Reputation: 1897

This way you can achieve the desired layout. Keep buttons in linear layout with vertical orientation.

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="2dp"
    android:orientation="horizontal"
    android:weightSum="3" >

    <TextView
        android:id="@+id/ap"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:singleLine="true"
        android:text="TEST" />

    <TextView
        android:id="@+id/st"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:singleLine="true"
        android:text="TEST" />

    <TextView
        android:id="@+id/tt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:singleLine="true"
        android:text="TEST" />
    <LinearLayout

      android:layout_width="wrap_content"
         android:layout_weight="1"
       android:layout_height="wrap_content"
     android:orientation="vertical">
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="36dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"

        android:text="Button" />

     <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="36dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"

        android:text="Button" />
       </LinearLayout>
</LinearLayout>

Upvotes: 2

Damien R.
Damien R.

Reputation: 3373

This should do the trick:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="2dp"
        android:orientation="horizontal"
        android:weightSum="3" >

        <TextView
            android:id="@+id/ap"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:singleLine="true"
            android:text="TEST" />

        <TextView
            android:id="@+id/st"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:singleLine="true"
            android:text="TEST" />

        <TextView
            android:id="@+id/tt"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:singleLine="true"
            android:text="TEST" />
        <LinearLayout
          android:id="@+id/linearLayout3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="vertical">
            <Button
              android:id="@+id/button1"
              android:layout_width="wrap_content"
              android:layout_height="36dp"
              android:layout_marginLeft="5dp"
              android:layout_marginTop="10dp"
              android:layout_weight="1"
              android:text="Button" />
            <Button
              android:id="@+id/button2"
              android:layout_width="wrap_content"
              android:layout_height="36dp"
              android:layout_marginLeft="5dp"
              android:layout_marginTop="10dp"
              android:layout_weight="1"
              android:text="Button" />
       </LinearLayout>
   </LinearLayout>
</RelativeLayout>

Upvotes: 1

Ranjit
Ranjit

Reputation: 5150

Try it with your button size:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="2dp"
    android:orientation="horizontal"
    android:weightSum="3" >

    <TextView
        android:id="@+id/ap"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:singleLine="true"
        android:text="TEST" />

    <TextView
        android:id="@+id/st"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:singleLine="true"
        android:text="TEST" />

    <TextView
        android:id="@+id/tt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:singleLine="true"
        android:text="TEST" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="36dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:text="Button" />

</LinearLayout>

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/linearLayout2"
    android:text="Button" />

 </RelativeLayout>

Upvotes: 5

thoma
thoma

Reputation: 583

Put that right after the first button:

<Button
    android:id="@+id/button2"
    android:layout_below="@id/button1"
    android:layout_width="wrap_content"
    android:layout_height="36dp"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="10dp"
    android:layout_weight="1"
    android:text="Button" />

Why using a LinearLayout inside a RelativeLayout? One of them is superflu.

Upvotes: 0

Related Questions