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 linea
In addition to urSus's answer, it's better to use a Space class instead of View
:
<Space android:id="@+id/fakeView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true"/>
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.
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
plannerEditLayout = (RelativeLayout) findViewById(R.id.plannerLayoutEdit);
plannerEditLayout.setGravity(Gravity.CENTER_VERTICAL);
plannerEditLayout.setLayoutParams(new RelativeLayout.LayoutParams(
(width / 2), height));
I use the same code for LinearLayout. Havent tried it out myslef for RelativeLayout. Give it a shot. Comment if it doesn't work.
Wrap the same thing with Relative layout which should work for you.
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="somebutton">
<TextView android:layout_width="fill_parent"
android:layout_height="Wrap_content"
android:layout_weight="1">
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:layout_width="match_parent"
android:id="@+id/button"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="somebutton">
<TextView android:layout_width="match_parent"
android:id="@+id/textview"
android:layout_height="Wrap_content"
android:layout_weight="1">
</LinearLayout>
In above example used LinearLayout, set widget layout_width to "match_parent" and provide weight(to specify area to be occupy).
This solves your problem.