Search code examples
androidandroid-studioratingbar

Align Numbers with RatingBar - Android


I am using a RatingBar in my app and assigning Numbers for Stars for the ease of user but alignment of these Numbers change whenever the device is changed. Although i've used different layouts for different screens but still on some mobile it is not formatting well.

What i want

enter image description here

what's showing on some mobiles

enter image description here


Solution

  • Use constraint layout as a parent and fit a linear layout. That should work fine.

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <RatingBar
            android:id="@+id/ratingBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:layout_editor_absoluteX="38dp"
            tools:layout_editor_absoluteY="192dp" />
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintBottom_toTopOf="@+id/ratingBar"
            app:layout_constraintEnd_toEndOf="@+id/ratingBar"
            app:layout_constraintStart_toStartOf="@+id/ratingBar">
    
            <TextView
                android:id="@+id/textView7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_vertical|center_horizontal"
                android:text="1"
                android:textAlignment="center" />
    
            <TextView
                android:id="@+id/textView15"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_vertical|center_horizontal"
                android:text="2"
                android:textAlignment="center" />
    
            <TextView
                android:id="@+id/textView17"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_vertical|center_horizontal"
                android:text="3"
                android:textAlignment="center" />
    
            <TextView
                android:id="@+id/textView18"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_vertical|center_horizontal"
                android:text="4"
                android:textAlignment="center" />
    
            <TextView
                android:id="@+id/textView19"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_vertical|center_horizontal"
                android:text="5"
                android:textAlignment="center" />
        </LinearLayout>
    </android.support.constraint.ConstraintLayout>
    

    enter image description here