Sjharrison
Sjharrison

Reputation: 729

Android Studio - Layout issue

I have the following android layout which has been split into multiple relative layouts. So far I've been okay with adjusting to not putting in fixed dimensions for the views however I've now become stuck

As you can see in the screenshot below I have a spinner, a spinner and a edittext all on one line, this at the moment is only repeated 4 times to give an idea of what I want to achieve however overall there will be 8

To illustrate how I want it I've had to put in fixed dimensions, however as soon as I put the spinner for example to wrap context it takes up the full width of the screen

My XML is as follows;

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="2.27"
    >


    <Spinner
        android:layout_width="100dp"
        android:layout_height="30dp"
        android:id="@+id/spinner"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:spinnerMode="dropdown" />

    <Spinner
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:id="@+id/spinner2"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/spinner"
        android:layout_toEndOf="@+id/spinner"
        android:layout_alignBottom="@+id/spinner"
        android:spinnerMode="dropdown" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/editText"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/spinner2"
        android:layout_toEndOf="@+id/spinner2"
        android:layout_alignBottom="@+id/spinner2"
        android:textSize="10sp" />
</RelativeLayout>

Does anyone know how to set this up the correct way?

Screenshot

Upvotes: 0

Views: 71

Answers (3)

Kunu
Kunu

Reputation: 5134

UseLinearLayout with layout_weight

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2.27>

<Spinner
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:id="@+id/spinner"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:spinnerMode="dropdown" />

<Spinner
    android:layout_width="0dp"
    android:layout_weight="2"
    android:layout_height="wrap_content"
    android:id="@+id/spinner2"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/spinner"
    android:layout_toEndOf="@+id/spinner"
    android:layout_alignBottom="@+id/spinner"
    android:spinnerMode="dropdown" />

<EditText
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/editText"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/spinner2"
    android:layout_toEndOf="@+id/spinner2"
    android:layout_alignBottom="@+id/spinner2"
    android:textSize="10sp" />

</LinearLayout>

Upvotes: 0

Ravi Gadipudi
Ravi Gadipudi

Reputation: 1455

please try this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="bottom"
android:orientation="horizontal">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="bottom">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="100dp"
        android:layout_height="30dp"
        android:spinnerMode="dropdown" />
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="bottom">

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:spinnerMode="dropdown" />
</LinearLayout>

<EditText
    android:id="@+id/editText"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:ems="10"
    android:gravity="bottom"
    android:inputType="number"
    android:textSize="10sp" />

</LinearLayout>

Upvotes: 0

Alok
Alok

Reputation: 266

Try using the LinearLayout with the desired weight .. it should solve your problem

Upvotes: 2

Related Questions