Hitesh saini Hitesh saini - 1 month ago 6
Android Question

How to center a TextView between two buttons?

I want to center my TextView between two buttons:

enter image description here

I take a RelativeLayout I don't know how is to do, below is my xml code please guide me where make a change to do this.

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent">
<ImageView
android:id = "@+id/my_image"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:background="@drawable/ic_launcher"
android:layout_above="@+id/layout_bottom"
android:layout_alignParentTop ="true" />

<RelativeLayout
android:id="@+id/layout_bottom"
android:layout_width="fill_parent"
android:layout_height = "50dp"
android:layout_alignParentBottom = "true">

<Button
android:id = "@+id/but_left"
android:layout_width = "80dp"
android:layout_height = "wrap_content"
android:text="&lt;"
android:layout_alignParentLeft = "true"/>

<TextView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text="@string/hello_world"
android:layout_toLeftOf = "@+id/but_right"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_alignBottom="@+id/but_right"
android:layout_toRightOf = "@id/but_left" />


<Button
android:id = "@id/but_right"
android:layout_width = "80dp"
android:layout_height = "wrap_content"
android:text="&gt;"
android:layout_alignParentRight = "true"/>

</RelativeLayout>
</RelativeLayout>

Answer
        <Button
            android:id = "@+id/but_left"
            android:layout_width = "80dp"
            android:layout_height = "wrap_content"
            android:text="Button1"
            android:layout_alignParentLeft = "true"/>

        <TextView
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            android:text="hello_world"
            android:layout_centerHorizontal="true"
            />
        <Button
            android:id = "@+id/but_right"
            android:layout_width = "80dp"
            android:layout_height = "wrap_content"
            android:text="Buton2 "
            android:layout_alignParentRight = "true"/>
Comments