Adjust width to half screen
android, android-relativelayout
Solution
This is a nice workaround if you precisely need half, when using RelativeLayout. What you do is, make a fake invisible view centered in parent, and have left button be alignParentLeft and alignRight to that fake view and the right button vice-versa
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<View android:id="@+id/fakeView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/fakeView"
android:layout_alignParentLeft="true"
android:text="Left"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/fakeView"
android:layout_alignParentRight="true"
android:text="Right"/>
</RelativeLayout>
Its really clever.
Problem
I came across this link : Assign width to half available screen width declaratively How to do the same in relative layout? Here's the code: Trying linearlayout inside relative layout. But this doesn't work. ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/bg" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <Button android:id="@+id/button5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="14dp" android:layout_weight="1" android:text="Button" /> <Button android:id="@+id/button6" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_weight="1" android:text="Button" /> <Button android:id="@+id/button4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_weight="1" android:text="UID" android:textColor="#ffe5f1f1" /> <EditText android:id="@+id/editText1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ffe5f1f1" android:hint="Enter UID" android:inputType="number" android:shadowColor="5" android:textColor="#ff009999" android:textStyle="bold" android:typeface="monospace" > <requestFocus /> </EditText> <Button android:id="@+id/button7" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:layout_alignParentLeft="true" android:layout_marginTop="18dp" android:text="Button" /> <Button android:id="@+id/button8" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/button7" android:layout_weight="1" android:layout_marginTop="16dp" android:text="Button" /> <Button android:id="@+id/button9" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:layout_alignParentLeft="true" android:layout_below="@+id/button8" android:layout_marginTop="17dp" android:text="Button" /> <EditText android:id="@+id/editText2" android:layout_width="50" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_weight="1" android:background="#ffe5f1f1" android:ems="10" android:hint="Enter Name" android:inputType="number" android:inputType="textPersonName" android:textColor="#ff009999" android:textStyle="bold" android:layout_weight="1" android:typeface="monospace" /> <EditText android:id="@+id/editText3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:ems="10" android:layout_weight="1" android:inputType="number" /> <EditText android:id="@+id/editText4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button7" android:layout_alignBottom="@+id/button7" android:layout_alignParentRight="true" android:ems="10" android:layout_weight="1" android:inputType="phone" /> <EditText android:id="@+id/editText5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button8" android:layout_alignBottom="@+id/button8" android:layout_alignParentRight="true" android:ems="10" android:layout_weight="1" android:inputType="textPersonName" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button9" android:layout_alignBottom="@+id/button9" android:layout_alignLeft="@+id/editText5" android:layout_weight="1" android:text="Male" /> <RadioButton android:id="@+id/radioButton1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/radioButton2" android:layout_alignBottom="@+id/radioButton2" android:layout_alignParentRight="true" android:layout_weight="1" android:text="Female" /> <Button android:id="@+id/button10" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/button9" android:layout_marginTop="31dp" android:layout_weight="1" android:text="Button" /> <Spinner android:id="@+id/spinDept" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button10" android:layout_alignBottom="@+id/button10" android:layout_alignLeft="@+id/radioButton2" android:layout_alignParentRight="true" android:layout_weight="1" android:background="#ff009999" /> </LinearLayout> </RelativeLayout> ```