Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: relative layout two buttons occupy all the available horizontal space

i have a relative layout and i have two buttons in it with texts "hello" and "world" respectively. i want these two buttons to lie adjacent to each other and equally occupy the whole horizontal space available.

i tried the following but didnt get expected output

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">

     <Button android:layout_height="wrap_content" android:layout_width="wrap_content"      android:text="@string/world" android:id="@+id/world"
     android:layout_alignParentRight="true"
     android:layout_toRightOf="@+id/hello"
     android:layout_alignTop="@+id/hello"
  />  


       <Button
          android:id="@+id/hello"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/hello"
          android:layout_alignParentLeft="true"
          android:layout_alignParentBottom="true"
        />
   </RelativeLayout>

i tried changing the android:layout_width of both children to fill_parent but that didnt work too.

i have a working solution of using LinearLayout with layout_weight set to 0.5 on both childs but i wanted to understand if there is way to do that in relative layout itself.

thanks,

like image 331
naiveCoder Avatar asked Mar 14 '11 19:03

naiveCoder


People also ask

How do you give a space between two buttons in android linear layout?

Use android:layout_marginTop="10.0dip" on second button.

How do I change the space between two buttons in android?

Notice the line that says android:layout_marginTop="10dip" which ensures that you leave a reasonable 10 dip space in between your buttons. Ofcourse, you can increase (or decrease) that space in between your buttons. That's your choice. Hope this answered your question satisfactorily.

Is it possible to evenly distribute buttons across the width of an android LinearLayout?

I suggest you use LinearLayout's weightSum attribute. Adding the tag android:weightSum="3" to your LinearLayout's xml declaration and then android:layout_weight="1" to your Buttons will result in the 3 buttons being evenly distributed.

Which type of layout allows the components in relative position in android?

Android Layout Code This Relative Layout consists of a Vertical LinearLayout within a Nested Horizontal LinearLayout along with a Child RelativeLayout.


1 Answers

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/hello"
        android:layout_toRightOf="@+id/view"
        android:text="First" />

    <View
        android:id="@+id/view"
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_centerHorizontal="true" />

    <Button
        android:id="@+id/hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/view"
        android:text="Second" />

</RelativeLayout>

Check

Note:

  • Even if the android:layout_width was fill_parent the result will not change.
  • What actually matters here is android:layout_alignParentRight and android:layout_alignParentLeft
like image 81
William Kinaan Avatar answered Nov 15 '22 18:11

William Kinaan