android LinearLayout

13,334

Solution 1

I want to lay two TextView to the left, and one button to the right inside a linear layout, is this possible?

Not with a single LinearLayout. You either need two LinearLayouts (one for a column of two TextViews on the left), or one RelativeLayout.

Is it possible to layout children that flows in different directions?

If by "different directions" you mean both vertical and horizontal simultaneously, a single LinearLayout can only go in one direction. Either use nested LinearLayouts or a RelativeLayout.

Solution 2

You need to use Table layout. look at table layout example in API demos.

Table layout - with 'stretch columns' = 1,
 -- Table row  - with width = fill_parent,
    --  Text View,
    --  Text View,
    --  Button,

This will keep your right button pushed to the right edge of the screen

Solution 3

Either use two linear layout(one with horizontal orientation and other with vertical orientation) or use Relative Layout. Relative Layout is stronger than linear and easy to use

Solution 4

Use Following:

 <LinearLayout
            android:id="@+id/widget43"
            android:layout_width="fill_parent"
            android:layout_height="100px"
            android:layout_margin="16dp"
            android:orientation="horizontal">

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

                <TextView
                    android:id="@+id/tc_buttom_text1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Time elapsed"></TextView>

                <TextView
                    android:id="@+id/tc_buttom_text2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="00:00:00 00"></TextView>
            </LinearLayout>

            <Button
                android:id="@+id/tc2_home"
                android:layout_width="70px"
                android:layout_height="wrap_content"
                android:layout_marginLeft="200px"
                android:layout_marginRight="10px"
                android:layout_weight="0.5"
                android:text="Home"></Button>
        </LinearLayout>

For More Android Layouts Tutorial and Example: http://www.viralandroid.com/2015/11/android-layouts.html

Share:
13,334
user121196
Author by

user121196

Updated on June 04, 2022

Comments

  • user121196
    user121196 almost 2 years

    I want to lay two TextView to the left, and one button to the right inside a linear layout, is this possible? The following is my code where I had to hardcode the leftMargin of the button, this is inflexible. Is it possible to layout children that flows in different directions?

    <LinearLayout
    android:id="@+id/widget43"
    android:layout_width="fill_parent"
    android:layout_height="100px"
    >
    <TextView
    android:id="@+id/tc_buttom_text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Time elapsed"
    >
    </TextView>
    <TextView
    android:id="@+id/tc_buttom_text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="00:00:00 00"
    >
    </TextView>
    <Button
    android:id="@+id/tc2_home"
    android:layout_width="70px"
    android:layout_height="wrap_content"
    android:layout_marginLeft="200px"
    android:layout_marginRight="10px"
    android:text="Home"
    android:layout_weight="0.5"
    >
    </Button>
    </LinearLayout>