Reputation: 956
How would one create a layout with a button on either side of a textview? This is what I've got so far:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/left_button"
android:layout_width="72dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="left" />
<TextView
android:id="@+id/center_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dip"
android:padding="10dip"
android:text="textview" />
<Button
android:id="@+id/right_button"
android:layout_width="72dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="right" />
</RelativeLayout>
The problem here is that the textview is centered. I want the textview to fill the complete space left. I tried setting its android:layout_width to fill_parent and removing the android:layout_centerHorizontal, but then it seems to overlap the buttons.
Upvotes: 2
Views: 2615
Reputation: 9872
It sounds like you would be better served by a LinearLayout, something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/left_button"
android:layout_width="72dip"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="left" />
<TextView
android:id="@+id/center_textview"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_weight="1"
android:padding="10dip"
android:text="textview" />
<Button
android:id="@+id/right_button"
android:layout_width="72dip"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="right" />
</RelativeLayout>
This way the three views will all be in a single horizontal line and the TextView will take up any remaining space after the buttons are laid out (see the android:layout_weight property).
Upvotes: 2
Reputation: 53647
Step-1
You need create first button in the left of the screen mention android:layout_alignParentLeft="true"
Step-2
create second button in the right of the screen mention android:layout_alignParentRight="true"
Step-3
Now create TextView and mention the it in the right of first buttona and left of second button by mentioning
android:layout_toRightOf="@id/left_button"
android:layout_toLeftOf="@+id/right_button"
XML Code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/left_button"
android:layout_width="72dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="left" />
<Button
android:id="@+id/right_button"
android:layout_width="72dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="right" />
<TextView
android:id="@+id/center_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_toRightOf="@id/left_button"
android:layout_toLeftOf="@+id/right_button"
android:padding="10dip"
android:text="textview" />
</RelativeLayout>
Upvotes: 5
Reputation: 8225
Not sure what you want, but you could use for example. layout_toRightOf. An example that aligns the button to parent left and then lays the views to the right of the left one.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/left_button"
android:layout_width="72dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="left" />
<TextView
android:id="@+id/center_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_toRightOf="@id/left_button"
android:padding="10dip"
android:text="textview" />
<Button
android:id="@+id/right_button"
android:layout_width="72dip"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/center_textview"
android:text="right" />
</RelativeLayout>
Upvotes: 0
Reputation: 18168
Try with a LinearLayout with orientation="horizontal", as oppose to a RelativeLayout.
Or if you want to do RelativeLayout, you can use the attributes Chandra mentioned (i.e., align the textview relative to the buttons, instead of relative to the parent container).
Upvotes: 0
Reputation: 19492
These attributes may solve your problem
android:layout_toLeftOf
android:layout_toRightOf
Upvotes: 0