Denis  Yakovenko
Denis Yakovenko

Reputation: 127

Center text in the TextView of a RecyclerView row

I have a RecyclerView in my app. Below's an xml code of its row:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="48dp">

    <TextView
        android:paddingLeft="-1.5dp"
        android:paddingTop="-1.5dp"
        android:textColor="#fff"
        android:id="@+id/text_with_icon"
        android:layout_gravity="center_vertical"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:text="A"
        android:textAlignment="center"
        android:textSize="22sp"
        android:gravity="center"
        android:background="@drawable/circle"
        android:layout_marginLeft="18dp"
        android:layout_marginStart="18dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:textColor="#000"
        android:gravity="center_vertical"
        android:textSize="16sp"
        android:textStyle="bold"
        android:layout_gravity="left|top"
        android:id="@+id/list_title"
        android:layout_marginLeft="72dp"
        android:padding="4dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dummy text"
        android:layout_marginStart="72dp"
        android:layout_toRightOf="@+id/text_with_icon" />

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="14dp"
        android:layout_marginLeft="77dp"
        android:textStyle="normal"
        android:textColor="#000"
        android:textSize="12sp"
        android:gravity="center_vertical"
        android:text="Lorem ipsum dolor sit amet, consectetur..."
        android:layout_alignLeft="@+id/list_title"
        android:layout_alignStart="@+id/list_title"
        android:layout_alignParentBottom="true"
        android:layout_gravity="left|bottom"
        android:layout_marginBottom="8dp"/>
</FrameLayout>

The Android Studio preview shows that it looks like on the following screenshot: custom row template

So, it looks like the letter in the red circle is well centered and here comes the first problem. In the screenshot from an emulator it is easy to notice that all the letters in those circles are not in the center. How can I center them? Emulator screenshot

And now the second issue. In the emulator screenshot if you look at the third item of the list ("Something3"), it has no description text below the text in bold. Is is possible to vertically center it in the row if there's no description? Circles in the list are backgrounds of the textview and are shapes in separate xml file (circle.xml):

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/primary"/>
</shape>

Upvotes: 0

Views: 2551

Answers (1)

Elltz
Elltz

Reputation: 10859

here is the more words, look at the TextView with id android:id="@+id/description" and check its Visiblity , it is set to gone if you set it to visible both TextView 's will be in the middle, but if gone your above TextView is always on top like what you see.. and the magic continues with Gravity.. (I'm referring to this solution)

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

<TextView
    android:id="@+id/text_with_icon"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="18dp"
    android:layout_marginStart="18dp"
    android:background="@drawable/circle"
    android:gravity="center"
    android:paddingLeft="-1.5dp"
    android:paddingTop="-1.5dp"
    android:text="A"
    android:textColor="#fff"
    android:textSize="22sp" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center_vertical"
    android:layout_toRightOf="@+id/text_with_icon"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/list_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="4dp"
        android:text="Dummy text"
        android:textColor="#000"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="14dp"
        android:text="Lorem ipsum dolor sit amet, consectetur..."
        android:textColor="#000"
        android:visibility="gone"
        android:textSize="12sp"
        android:textStyle="normal" />
</LinearLayout>

in your getView() or java side, check if the string is empty or not so

if(!TextView.getText().toString().isEmpty()){ // if it is not empty
    TextView.setVisibility(0); // zero means visible..
}

if you are targeting below api 9 the put this in the if statement TextView.getText().toString().length() != 0

Upvotes: 1

Related Questions