0

Textview is only center_horizontal in case of v4.4 devices at upper side of single row and In case of v4.1 and v5.0, textView is exactly centered. I had checked these sorts of question in StackOverflow but no answer solved the issue. Please, check out my code.

single_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="4dp"
        android:layout_margin="4dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:paddingLeft="10dp">
            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/drawable_oval" />

            <TextView
                android:gravity="center"
                android:paddingBottom="3dp"
                android:paddingTop="3dp"
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/def_total_schedule"
                android:textSize="11sp"
                android:layout_alignBottom="@+id/imageView1"
                android:layout_toRightOf="@+id/imageView1"
                android:layout_toLeftOf="@+id/imageView2"
                android:layout_toStartOf="@+id/imageView2" />

            <TextView
                android:textStyle="bold"
                android:textSize="15sp"
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/imageView1"
                android:text="@string/default_schedule"
                android:gravity="center"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:layout_toLeftOf="@+id/imageView2"
                android:layout_toRightOf="@+id/imageView1"
                android:layout_toEndOf="@+id/imageView1" />

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingRight="10dp"
                android:src="@mipmap/ic_location"
                android:layout_alignTop="@+id/textView1"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_alignBottom="@+id/imageView1" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_alignParentStart="true"
                android:text="@string/sunday"
                android:textColor="@color/icons"
                android:textStyle="bold"
                android:id="@+id/textViewDAY"
                android:layout_alignTop="@+id/imageView1"
                android:layout_alignBottom="@+id/imageView1"
                android:layout_alignLeft="@+id/imageView1"
                android:layout_alignRight="@+id/imageView1"
                android:gravity="center"
                />

            <TextView
                android:textStyle="bold"
                android:textSize="15sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/default_schedule"
                android:id="@+id/textView"
                android:gravity="center"
                android:layout_below="@+id/textView1"
                android:layout_toRightOf="@+id/imageView1"
                android:layout_toLeftOf="@+id/imageView2"
                android:layout_toStartOf="@+id/imageView2" />
        </RelativeLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>
jazzbpn
  • 6,441
  • 16
  • 63
  • 99
  • why are you using gravity inside a RelativeLayout? and which TextView exactly are you talking about? I don't see any center_horizontal – M D P Nov 07 '15 at 15:07
  • android:id="@+id/textViewDAY" (SecondLast) . I mean the textView in v4.4 appears like center_horizontal (little bit above from center) – jazzbpn Nov 07 '15 at 15:34

1 Answers1

0

Finally Done

Here, We have to make the inner RelativeLayout and use the property android:layout_centerInParent="true" to TextView.

       <RelativeLayout
            android:id="@+id/child_relative_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true">

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/drawable_oval" />

            <TextView
                android:id="@+id/textViewDAY"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="@string/sunday"
                android:textColor="@color/icons"
                android:textStyle="bold" />

        </RelativeLayout>
jazzbpn
  • 6,441
  • 16
  • 63
  • 99