2

How to align a TextView, Spinner and Button in same row in android ?

My layout xmls as follows

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"  
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" 
android:gravity="center">


<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
     >

    <TextView android:id="@+id/textView1" 
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:text="Show"
        android:gravity="center" 
        android:layout_weight="1"
        android:textColor="#CD2134"  
        android:textStyle="bold"  />


    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:layout_marginBottom="15dp"
        android:prompt="@string/prompt"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Get"
        android:layout_weight="1"
        android:onClick="button_click"/>


</LinearLayout>


  </LinearLayout>

output of the above layout as follows

enter image description here

I want just like below image

enter image description here

thanks in advance

Riskhan
  • 4,434
  • 12
  • 50
  • 76

4 Answers4

12

Try this..

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Show"
        android:textColor="#CD2134"
        android:textStyle="bold" />

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

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="button_click"
        android:text="Get" />
</LinearLayout>
Hariharan
  • 24,741
  • 6
  • 50
  • 54
2

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:text="Show"
    android:textColor="#CD2134"
    android:textStyle="bold" />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_gravity="center_vertical"
    android:prompt="@string/prompt" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="button_click"
    android:layout_gravity="center_vertical"
    android:text="Get" />

Liem Vo
  • 5,149
  • 2
  • 18
  • 16
1

Try this

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"  
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" 
android:gravity="center">

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:gravity="center"
    android:text="Show"
    android:textColor="#CD2134"
    android:textStyle="bold" />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_marginBottom="15dp"
/>

<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:onClick="button_click"
    android:text="Get" />

Hope this will solve the issue

Systematix Infotech
  • 2,345
  • 1
  • 14
  • 31
0
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:weightSum="3"
android:orientation="horizontal" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" 
   />

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

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" 
    android:text="Get" />