Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I align vertically centered a Spinner and a TextView

Tags:

android

I have a Spinner and a TextView in my LinearLayour.

I am trying to align the Spinner and a TextView vertically so that the center of both in on the same line:

enter image description here

This is my XML for that element:

 <LinearLayout
    android:id="@+id/dropdownlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center" />

    <TextView
        android:id="@+id/ok1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:background="@drawable/white_box"
        android:gravity="center"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="@string/ok"
        android:textColor="@android:color/black"
        android:textSize="@dimen/normale" />

</LinearLayout>

Tried this code as well, same result:

  <RelativeLayout
    android:id="@+id/dropdownlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_gravity="center"
        android:gravity="center" />

    <TextView
        android:id="@+id/ok1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/spinner1"
        android:background="@drawable/white_box"
        android:gravity="center"
        android:padding="10dp"
        android:text="@string/ok"
        android:textColor="@android:color/black"
        android:textSize="@dimen/normale" />
</RelativeLayout>
like image 996
Lisa Anne Avatar asked Feb 15 '23 03:02

Lisa Anne


2 Answers

Well note that you can't use RelativeLayout with Gravity or Layout_gravity

an example of how i do things. This works perfectly (it's at the right of a list view cell centerred verticaly)

<RelativeLayout
        android:id="@+id/right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_margin="4dp" >

        <TextView
            android:id="@+id/annuaire_distance_etablissement"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginRight="4dp"
            android:layout_toLeftOf="@+id/arrow_img"
            android:singleLine="true"
            android:textColor="@color/clr_main_green"
            android:textSize="12sp" />

        <ImageView
            android:id="@+id/arrow_img"
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="4dp"
            android:src="@drawable/icone_fleche" />
    </RelativeLayout>
like image 98
An-droid Avatar answered Feb 17 '23 16:02

An-droid


Try this in Relative Layout like this

[<RelativeLayout
    android:id="@+id/dropdownlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="250dp"
         android:gravity="center"
        android:background="@android:drawable/btn_dropdown"
        android:layout_centerVertical="true"
        android:layout_height="40dp"
        android:layout_gravity="center" />

    <TextView
        android:id="@+id/ok1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/white_box"
        android:gravity="center"
        android:layout_centerVertical="true"
        android:paddingLeft="10dp"
        android:layout_toRightOf="@+id/spinner1"
        android:paddingRight="10dp"
        android:text="@string/ok"
        android:textColor="@android:color/black"
        android:textSize="@dimen/normale" />
</RelativeLayout>]

enter image description here

like image 29
Jitender Dev Avatar answered Feb 17 '23 17:02

Jitender Dev