How to show rating bar from main activity java android application — Java Android
This is how you can show an Android Rating Bar
message from a Main Activity in Java Android Studio:
This example demonstrates how to use the rating bar in android.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
How to use RatingBar in android?
Step 2 — “Add the following code to res/layout/activity_main.xml.“
I worked in simple relative layout method for only give basic example.
In the below code, we have declared Rating bar and number of stars “5”, Rating “4.5”, Step Size “0.25”
android:numStars=”5"
android:rating=”4.5"
android:stepSize=”0.25"
LAYOUT FILE CODE IN XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.yourname.yourandroidstuido.MainActivity">
<RatingBar
android:id="@+id/idratingBar"
android:layout_marginTop="150dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:numStars="5"
android:rating="4.5"
android:stepSize="0.25" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:text="Give Rating Now"
android:onClick="onBtnClick"
android:id="@+id/button"
android:layout_below="@+id/idratingBar"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="600dp"
android:gravity="center"
android:text="5S JAVA AND KOTLIN LEARNING" />
</RelativeLayout>
Third Step “MainActivity.java” java file − Add the following code to src/MainActivity.java
JAVA CODE IN MAIN ACTIVITY:
package com.example.andy.myapplication;import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button btn;
RatingBar ratingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.button);
ratingBar = findViewById(R.id.idratingBar);
}
public void onBtnClick(View v) {
float ratingvalue = ratingBar.getRating();
Toast.makeText(this, "Your rating is: " +ratingvalue, Toast.LENGTH_LONG).show();
}
}
As shown, don’t forget to call methods, click alt + enter for implement methods.
Review Application
Fourth Step Results and How to Work Rating Bar !
The above result we have selected rating 3.0 and printed as 3.0
If you have any question write comments I will reply.
Have a nice day.
If you need Google AdMob Alternatives you can check out Start Ad Platfrom.
Regards
5S JAVA AND KOTLIN LEARNING