In this comprehensive tutorial, you will learn how to develop an Android application that incorporates a RatingBar using both Kotlin and Java. The tutorial provides a detailed, step-by-step guide to help you set up the layout XML file and implement functionality to manage user interactions with the RatingBar effectively.
Submitted on July 07, 2024
Creating an Android app with a RatingBar in both Kotlin and Java involves setting up the layout XML file and then implementing functionality in the respective language for handling user interactions and displaying the rating. Below are step-by-step instructions for both Kotlin and Java:
Create Project: Start a new Android Studio project with an Empty Activity.
Layout (‘res/layout/activity_main.xml’): Design the main activity layout with a RatingBar and a TextView to display the rating.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="0"
android:stepSize="1"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/tvRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rating: 0"
android:textSize="18sp"
android:layout_below="@id/ratingBar"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"/>
</RelativeLayout>
Implement MainActivity (‘MainActivity.kt’):
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.TextView;
class MainActivity : AppCompatActivity() {
private lateinit var ratingBar: RatingBar
private lateinit var tvRating: TextView
@Override
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ratingBar = findViewById(R.id.ratingBar)
tvRating = findViewById(R.id.tvRating)
ratingBar.setOnRatingBarChangeListener { _, rating, _ ->
tvRating.text = "Rating: $rating"
}
}
}
Implement MainActivity (MainActivity.java):
Handle RatingBar changes and update the TextView accordingly.
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private RatingBar ratingBar;
private TextView tvRating;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ratingBar = findViewById(R.id.ratingBar);
tvRating = findViewById(R.id.tvRating);
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
tvRating.setText("Rating: " + rating);
}
});
}
}
By following these steps, you can create a simple Android app with a RatingBar that updates a TextView to display the selected rating, demonstrating how to handle user input and update UI elements accordingly in both Kotlin and Java.