Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show labels next to spinners in a relativeLayout

I have the following RelativeLayout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<RadioGroup
    <!--stuff-->
</RadioGroup>

<Spinner
android:id="@+id/colour_spinner"
android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_below="@+id/radioGroup1" />

 <TextView
    android:id="@+id/colourLabel"
    android:text="@string/label_colour"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/radioGroup1"
    android:layout_toLeftOf="@+id/colour_spinner"/>

<Spinner
android:id="@+id/country_spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_below="@+id/colour_spinner" />

<TextView
    android:id="@+id/countryLabel"
    android:text="@string/label_country"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/colour_spinner"
    android:layout_toLeftOf="@+id/country_spinner"/>

<Spinner
    android:id="@+id/stadium_spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_below="@+id/country_spinner" />

<TextView
    android:id="@+id/stadiumLabel"
    android:text="@string/label_stadium"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/country_spinner"
    android:layout_toLeftOf="@+id/stadium_spinner"/>

Hopefully it's obvious what I'm trying to do here. Three spinners, each below the one before, and a label to the left of each, ideally all lined up neatly.

The result I'm getting at the moment is that only the spinners show on the screen, and I get no text labels at all. What am I doing wrong here? I suspect it's to do with my layout width/height settings?

like image 640
f1dave Avatar asked Nov 20 '25 12:11

f1dave


1 Answers

Enclose each spinner and label inside a horizontal LinearLayout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<RadioGroup
    android:id="@+id/radioGroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <!-- stuff -->
</RadioGroup>

<LinearLayout
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/radioGroup"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/colourLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/label_colour" />

    <Spinner
        android:id="@+id/colour_spinner"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</LinearLayout>

<LinearLayout
    android:id="@+id/spinner2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/spinner1"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/countryLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/label_country" />

    <Spinner
        android:id="@+id/country_spinner"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</LinearLayout>

<LinearLayout
    android:id="@+id/spinner3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/spinner2"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/stadiumLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/label_stadium" />

    <Spinner
        android:id="@+id/stadium_spinner"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

</RelativeLayout>
like image 56
Marcin S. Avatar answered Nov 23 '25 01:11

Marcin S.