Question

layout difference between 4.2.2 and 2.3.3

The image on the right (2.3.3) presents my view as i would like it to, however i can't get it to look right on 4.2.2. Notice how the text in the textview is not vertically centered. I've tried both in the emulator and on Galaxy S4. Also, the eclipse ADT preview of XML files shows it centered correctly.

The view is used in a listview.

How do i center the text in my textview so it looks the same on all android versions?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="16dp" >

    <ImageView android:id="@+id/fylke_vapen"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:gravity="center"

        android:scaleType="fitCenter"

        android:src="@drawable/fylke_buskerud" />

    <TextView android:id="@+id/fylke_navn"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/fylke_vapen"
        android:layout_alignBottom="@id/fylke_vapen"
        android:layout_alignTop="@id/fylke_vapen"
        android:gravity="center_vertical"

        android:paddingLeft="16dp"

        android:textSize="24sp"
        android:maxLines="1"
        android:text="Placeholder text" />

    <CheckBox android:id="@+id/star"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/fylke_vapen"
        android:layout_alignBottom="@id/fylke_vapen"
        android:gravity="center"

        android:button="@drawable/star_checkbox"
        android:focusable="false" /> 

</RelativeLayout>
Was it helpful?

Solution 2

Well, i solved my issue by wrapping the textview in a linearlayout which centers the textview via gravity.

There seems to be a bug where you can't match_parent on textview within a listview on 4.2.2 (i've not tested any other versions than 4.2.2 and 2.3.3)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="16dp" >

    <ImageView android:id="@+id/fylke_vapen"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_alignParentLeft="true"
        android:scaleType="fitCenter"
        android:src="@drawable/fylke_buskerud" />

    <CheckBox android:id="@+id/star"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_alignParentRight="true"
        android:button="@drawable/star_checkbox"
        android:focusable="false" /> 

    <LinearLayout 
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_toRightOf="@id/fylke_vapen"
        android:layout_toLeftOf="@id/star"
        android:layout_alignBottom="@id/fylke_vapen"
        android:layout_alignTop="@id/fylke_vapen"
        android:paddingLeft="16dp"
        android:gravity="left|center" >

        <TextView android:id="@+id/fylke_navn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:maxLines="1"
            android:text="Placeholder text" />
    </LinearLayout>
</RelativeLayout>

OTHER TIPS

You should to remove android:layout_alignBottom and android:layout_alignTop attributes.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/parent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:padding="16dp" >

    <ImageView android:id="@+id/fylke_vapen"
               android:layout_width="wrap_content"
               android:layout_height="match_parent"
               android:layout_alignParentLeft="true"
               android:scaleType="fitCenter"
               android:src="@drawable/userpic_sidebar_default" />

    <TextView android:id="@+id/fylke_navn"
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:layout_toRightOf="@id/fylke_vapen"
              android:layout_toLeftOf="@id/star"
              android:gravity="center_vertical"
              android:paddingLeft="16dp"
              android:textSize="24sp"
              android:maxLines="1"
              android:text="Placeholder text" />

    <CheckBox android:id="@+id/star"
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:layout_alignParentRight="true"
              android:button="@drawable/star_checkbox"
              android:gravity="center"
              android:focusable="false" />

</RelativeLayout>
// try this way,hope this will help you...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    android:padding="10dp" >

    <ImageView
        android:id="@+id/fylke_vapen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_launcher"/>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center">
        <TextView
            android:id="@+id/fylke_navn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:maxLines="1"
            android:gravity="center"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="Placeholder text" />
    </LinearLayout>


    <CheckBox
        android:id="@+id/star"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/ic_launcher"
        android:focusable="false" />

</LinearLayout>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top