I'm struggling with a weird trouble with linearlayout about setting a TextView and a spinner on the same row (side to side) in my layout.
I've already tried all the solutions found here on the forum none of them seemed to work.
I tried this: How to layout elements side by side on a Linear Layout and apply proper spacing and alignment
This: Align textview and spinner on same row
This: How can one align spinners right to left?
And this: How to move spinner in LinearLayout to right?
The main issue is that if i set android:orientation="horizontal" all my layout gets distorted and buggy.
Of course setting android:weightSum="1" on both items I want to align, is not working....
Here is my activity_main.xml layout code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#191414"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/aboutLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal">
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="fitXY"
android:layout_gravity="center_horizontal"
android:contentDescription="Header picture"
android:importantForAccessibility="no"
android:src="@drawable/spoty"
android:layout_marginBottom="15dp"/>
<Switch
android:id="@+id/Switchs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Smart" />
<TextView
android:id="@+id/switch_text"
android:text="@string/delete_cache"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:gravity="start"
android:textStyle="bold"
android:layout_marginEnd="45dp"
android:layout_marginBottom="10dp"/>
<TextView
android:id="@+id/main_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:gravity="start"
android:textStyle="bold"
android:layout_marginBottom="10dp"/>
<TextView
android:id="@+id/days_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/soemstring"
android:layout_weight="1"
android:textStyle="bold" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/spinner1"
android:entries="@array/days">
</Spinner>
<TextView
android:id="@+id/size_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="@string/someotherstring"
android:gravity="start"
android:textStyle="bold"
android:layout_marginBottom="5dp"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner2"
android:gravity="start"
android:entries="@array/sizes"
android:layout_marginBottom="5dp">
</Spinner>
<TextView
android:id="@+id/notes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="@string/notes"
android:gravity="start"
android:textStyle="bold"
android:layout_marginEnd="15dp"
android:layout_marginBottom="25dp"/>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="@string/sometext"
android:id="@+id/button1"
android:textStyle="bold"
android:layout_gravity="center"
android:background="@drawable/btn_round"
android:layout_marginBottom="5dp"/>
</LinearLayout>
</RelativeLayout>
What can to to fix all of this? Any suggestion is highly appreciated!
If I got the idea behind your sample, you will end up using nested LinearLayout's - upper one with vertical orientation and nested one with horizontal implementation - which is wrong btw, because you should be using ConstraintLayout. Anyways, I believe in your case it'll go like this:
<?xml version="1.0" encoding="utf-8"?>
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/aboutLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/days_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="something"
android:textStyle="bold" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/days" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
If you are using horizontal orientation and your items layout_width are wrap_content, they will simply go one by one. If you put weights on them, don't forget to set their layout_width to 0dp however I suspect you only need former one as top level LinearLayout will put your pair of views in the center of the screen, like this:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With