Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get two fields, side by side in relative layout (android)?

Tags:

android

layout

I know that you can use weight parameters for linear layout in order to make two fields align nicely. What I want to do is I want to make sure that left half of the screen is used by one text field and other half is used by other text field (I am talking about width). How to do so?

like image 585
user3178137 Avatar asked Jan 26 '14 20:01

user3178137


People also ask

What is RelativeLayout and LinearLayout in Android?

LinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets.

Can we use RelativeLayout inside ConstraintLayout?

You can't use relative layout directly inside constraint layout. Intention of ConstraintLayout is to optimize and flatten the view hierarchy of your layouts by applying some rules to each view to avoid nesting.

How do you use relative layout?

RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on.


1 Answers

Use a "hidden view", with no height or width, in the center and put the text views on either side. Use parent align to set left and right.

<RelativeLayout 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content">

   <View android:id="@+id/dummy"
       android:layout_height="0dp" 
       android:layout_width="0dp"
       android:layout_centerInParent="true"/>

   <TextView
       android:layout_alignRight="@id/dummy"
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"
       android:layout_alignParentLeft="true"/>

   <TextView
       android:layout_alignLeft="@id/dummy"
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"
       android:layout_alignParentRight="true"/>

</RelativeLayout>
like image 172
Simon Avatar answered Oct 01 '22 22:10

Simon