Rajubhai Rathod Rajubhai Rathod - 5 months ago 9
Android Question

Center Horizontal Text

I am using CardView for list my quote with author image. I am trying to set quote in center horizontal in list. I have tried center gravity and related method. But I am not success. My current view is showing like below

Current

enter image description here

Need

enter image description here

My XML is like below

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">


<android.support.v7.widget.CardView

android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
app:cardElevation="4dp"
app:cardCornerRadius="6dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:id="@+id/quoteActionView"
android:clickable="true"
android:background="?attr/selectableItemBackground"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:visibility="visible"
android:id="@+id/imageAuthorView"
android:scaleType="fitXY"
android:layout_width="150dp"
android:layout_height="150dp" />
<TextView
android:visibility="gone"
android:id="@+id/textIndxQuote"
android:padding="8dp"
android:gravity="center"
android:background="#444444"
android:textColor="@android:color/white"
android:textStyle="bold|italic"
android:textSize="16sp"
android:layout_width="48dp"
android:layout_height="match_parent" />

<LinearLayout
android:layout_margin="8dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/textQuote"
android:scrollHorizontally="true"
android:ellipsize="end"
android:maxLines="2"
android:textSize="40sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

</LinearLayout>

</android.support.v7.widget.CardView>




Please check and let me know what I am missing.

Thanks

Answer

Change your last LinearLayout to this:

   <LinearLayout
      android:layout_margin="8dp"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_gravity="center"
      android:layout_height="wrap_content">