Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

Creating a RatingBar in Android

The RatingBar widget in Android is used to get the rating from app users. The widget shows a set of stars to represent the rating and allows users to choose the desired number of stars.

Here's a step-by-step guide on how to create and use a RatingBar in an Android app:

1. Design the Layout:

Open the res/layout/activity_main.xml and add the RatingBar widget along with a Button to submit the rating and a TextView to display it:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    android:gravity="center">

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:stepSize="1" />

    <Button
        android:id="@+id/submitRatingButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit Rating"
        android:layout_marginTop="24dp" />

    <TextView
        android:id="@+id/ratingDisplayText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="Your rating: Not submitted" />

</LinearLayout>

In this layout:

  • numStars is set to 5 to display 5 stars.
  • stepSize is set to 1 which means the user can only select whole stars. If you want the user to be able to select half-stars, you can set stepSize to 0.5.

2. Implement Logic in MainActivity:

Open the MainActivity.java file and add logic to handle the rating submission:

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final RatingBar ratingBar = findViewById(R.id.ratingBar);
        Button submitButton = findViewById(R.id.submitRatingButton);
        final TextView ratingDisplayText = findViewById(R.id.ratingDisplayText);

        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                float ratingValue = ratingBar.getRating();
                ratingDisplayText.setText(String.format("Your rating: %.1f", ratingValue));
            }
        });
    }
}

3. Run the App:

Launch the app on an emulator or a real device. Users can select their desired rating by clicking on the stars. When the "Submit Rating" button is clicked, the rating will be displayed below the button.

This is a basic example of a RatingBar in Android. Depending on your needs, you can customize its appearance, add listeners for rating changes, or integrate with other components or services to store or process the ratings further.

  1. Customizing RatingBar in Android example:

    You can customize the appearance of a RatingBar in Android by modifying attributes such as android:progressDrawable, android:indeterminateDrawable, and android:secondaryProgressDrawable in XML or programmatically. For example:

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:progressDrawable="@drawable/custom_rating_bar"
        android:numStars="5"
        android:rating="3.5"/>
    
  2. Setting up RatingBar in Android XML layout:

    To set up a basic RatingBar in XML, you can use the following code:

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:rating="3.5"/>
    
  3. Handling RatingBar changes in Android app:

    You can handle changes to the RatingBar by setting an OnRatingBarChangeListener. For example:

    RatingBar ratingBar = findViewById(R.id.ratingBar);
    ratingBar.setOnRatingBarChangeListener((ratingBar, rating, fromUser) -> {
        // Handle rating changes
    });
    
  4. Styling and theming RatingBar in Android:

    You can apply styles and themes to the RatingBar to customize its appearance. Define styles in your styles.xml file and apply them to the RatingBar:

    <RatingBar
        style="@style/CustomRatingBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:rating="3.5"/>
    

    In styles.xml:

    <style name="CustomRatingBarStyle">
        <!-- Customize rating bar attributes -->
    </style>
    
  5. RatingBar with half-star ratings in Android:

    To enable half-star ratings, set the stepSize attribute in XML or programmatically:

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:stepSize="0.5"
        android:rating="3.5"/>
    
  6. Setting initial rating in Android RatingBar:

    To set the initial rating programmatically, use the setRating method:

    RatingBar ratingBar = findViewById(R.id.ratingBar);
    ratingBar.setRating(4.0f);
    
  7. Custom drawables for RatingBar in Android:

    Customize the drawables used by the RatingBar by specifying your own drawables in the XML or programmatically setting drawables using the setProgressDrawable method:

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:progressDrawable="@drawable/custom_rating_drawable"
        android:numStars="5"
        android:rating="3.5"/>
    

    In this example, @drawable/custom_rating_drawable refers to a custom drawable resource.