androidxmlandroid-linearlayoutratingbar

How to center a RatingBar in Android


I have created a custom RatingBar as described in one of the tutorials given here on stack-overflow and it's looking great, the problem is with the centering of the image. here is what I've got: enter image description here

As you can see the TextView is centered vertically in the LinearLayout, but the RatingBar is at the top of it. Here is my xml code of this image:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:foregroundGravity="center" >

    <LinearLayout
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:gravity="center" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <RatingBar
            android:id="@+id/ratingBar1"
            style="@style/starsRatingBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:numStars="5"
            android:rating="2.3"
            android:stepSize="1" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:src="@drawable/ic_launcher" />

    </LinearLayout>

</ScrollView>

Here is the code of the styling:

<style name="starsRatingBar" parent="@android:style/Widget.RatingBar">
        <item name="android:progressDrawable">@drawable/star_ratingbar_full</item>
        <item name="android:minHeight">36dip</item>
        <item name="android:maxHeight">36dip</item>
    </style>

Any ideas in the RatingBar would be apprisiated!

THANKS


Solution

  • Ok, I think I've got it.

    The key issue is the ScrollView: it automatically makes things compact, unless you specifically tell it otherwise. In the scroll view, set

    android:fillViewport="true"
    

    to make it use the space available.