user3432257
user3432257

Reputation: 415

Android RelativeLayout how to align two views horizontally

This is my 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="fill_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >

    <TextView
        android:id="@+id/tv_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="Your Text" />

    <Spinner
        android:id="@+id/s_country"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/tv_id"
        android:layout_toLeftOf="@+id/city"
        android:entries="@array/country_spinner_values" />

    <Spinner
        android:id="@+id/city"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/tv_id"
        android:entries="@array/city_spinner_values" />

</RelativeLayout>

and this is the result: enter image description here

As you see the Spinner in the left takes more space than the Spinner in the right. How can I make each one of them takes the same space, in other words, the same width for both of them?

Note: I would like not to use LinearLayout

Upvotes: 0

Views: 3247

Answers (1)

William Kinaan
William Kinaan

Reputation: 28799

What about this?

<?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="fill_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >

    <TextView
        android:id="@+id/tv_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="Belongs to:" />

    <Spinner
        android:id="@+id/s_country"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/tv_id"
        android:layout_toLeftOf="@+id/view"
        android:entries="@array/country_spinner_values" />

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

    <Spinner
        android:id="@+id/city"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/tv_id"
        android:layout_toRightOf="@+id/view"
        android:entries="@array/city_spinner_values" />

</RelativeLayout>

enter image description here

Upvotes: 2

Related Questions