3

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> 

6 Answers 6

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

Comments

1

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

Comments

0

LinearLayout has an orientation attribute. Try something like this:

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

Comments

0

I use two LinearLayouts like this:

<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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" android:background="#0099cc" tools:context=".FullscreenActivity"> <!-- The primary full-screen view. This can be replaced with whatever view is needed to present your content, e.g. VideoView, SurfaceView, TextureView, etc. --> <TextView android:id="@+id/fullscreen_content" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:keepScreenOn="true" android:text="@string/dummy_content" android:textColor="#33b5e5" android:textSize="50sp" android:textStyle="bold" /> <!-- This FrameLayout insets its children based on system windows using android:fitsSystemWindows. --> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/background_dark" android:fitsSystemWindows="true"> <LinearLayout android:id="@+id/fullscreen_content_controls" style="?metaButtonBarStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal" android:background="@color/black_overlay" android:baselineAligned="false" android:orientation="vertical" tools:ignore="UselessParent"> <LinearLayout android:id="@+id/fullscreen_choose_controls" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <TextView android:id="@+id/fullscreen_esop" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:gravity="center" android:text="@string/vs_esop" android:textAllCaps="false" android:textColor="@android:color/holo_blue_dark" android:textSize="50sp" android:textStyle="bold" /> <Button android:id="@+id/dummy_button" style="?metaButtonBarButtonStyle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="0.3" android:ellipsize="end" android:gravity="center" android:text="@string/dummy_button" android:textColor="@android:color/holo_red_dark" android:textSize="20sp" android:textStyle="bold" /> <TextView android:id="@+id/fullscreen_android" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:gravity="center" android:text="@string/vs_android" android:textAllCaps="false" android:textColor="@android:color/holo_blue_dark" android:textSize="50sp" android:textStyle="bold" /> </LinearLayout> <TextView android:id="@+id/fullscreen_make_your_choice" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_gravity="center" android:layout_weight="1" android:gravity="center" android:text="@string/vs_make_your_choice" android:textColor="@android:color/holo_red_dark" android:textSize="50sp" android:textStyle="bold" /> </LinearLayout> </FrameLayout> </FrameLayout> 

Demo

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.