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:
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>
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>
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>]
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