Home > Back-end >  How to align text of textview in RecyclerView at the bottom
How to align text of textview in RecyclerView at the bottom

Time:03-23

I am creating a list of text sizes UI that will have the different size of same text rendering in the RecyclerView.

The requirement is, text item that is rendering in the RecyclerView shall render from the bottom of the view, but its rendering from the top. Here is my code:

RecyclerView declaration:

<RelativeLayout
        android:id="@ id/sizes_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:elevation="30dp"
        android:paddingVertical="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <RelativeLayout
            android:id="@ id/size_options"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@ id/subtitles"
            android:layout_gravity="center_horizontal"
            android:layout_marginHorizontal="20dp"
            android:layout_marginTop="16dp"
            android:orientation="horizontal">

            <TextView
                android:id="@ id/size_textview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawablePadding="16dp"
                android:gravity="center_vertical"
                android:minWidth="50dp"
                android:text="@string/size"
                android:textColor="@color/white"
                android:textStyle="bold"
                tools:textColor="@color/black" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@ id/text_track_size_list_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toEndOf="@ id/size_textview"
                android:orientation="horizontal"
                app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
        </RelativeLayout>
    </RelativeLayout>

View Item declaration:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@ id/focus_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="10dp">

    <TextView
        android:id="@ id/row_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:paddingHorizontal="10dp"
        android:paddingVertical="7dp"
        android:textAllCaps="false"
        android:textColor="@drawable/text_size_color"
        tools:text="Aa" />
</FrameLayout>

Bind View Holder method:

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val textSizeOption: TextSizeOption = extSizeOptions[position]
        holder.focusView.tag = textSizeOption

        holder.rowItem.textSize = textSizeOption.fontSize
        if(mSelectedFormat != null && mSelectedFormat == textSizeOption.id) {
            holder.focusView.isSelected = true
        } else {
            holder.focusView.isSelected = (position == 0 && mSelectedFormat == null)
        }

    }

I have tried multiple things like wrapping the item view in the Relative Layout and giving alignParentBottom, etc but that didn't work as well.

Here is the screenshot of how its looking right now: Sizes text top aligning

Even the Size label is not centre aligning as its given in the code. Not sure if I am missing something, but any help will be super appreciated

CodePudding user response:

Modifying your view item like this will solve your problem

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

    <TextView
        android:layout_gravity="center_vertical"
        android:id="@ id/row_item"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@drawable/text_size_color"
        android:text="Aa" />
</LinearLayout>
  • Related