Open In App

Android Jetpack Compose – Implement Easy Rating Dialog

Improve
Improve
Like Article
Like
Save
Share
Report

Many times in android applications we can get to see that they ask for users to rate their application and share their reviews about the application. In this article, we will take a look at How to implement an Easy Rating dialog in android applications using Jetpack Compose. Using the Easy Rating Dialog box we will display a prompt to the user to rate the application. A sample video is given below to get an idea about what we are going to do in this article.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Adding dependency in build.gradle

Navigate to app>build.gradle file and add the below dependency to it in the dependencies section. 

implementation 'com.github.fernandodev.easyratingdialog:easyratingdialog:1.1.2'

After adding the dependency simply sync your project to install it. 

Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.example.newcanaryproject
  
import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.webkit.WebSettings.TextSize
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.content.ContextCompat
import com.example.newcanaryproject.ui.theme.NewCanaryProjectTheme
import com.example.newcanaryproject.ui.theme.greenColor
import com.github.fernandodev.easyratingdialog.library.EasyRatingDialog
  
class MainActivity : ComponentActivity() {
  
    override fun onCreate(savedInstanceState: Bundle?) {
  
        super.onCreate(savedInstanceState)
        setContent {
            NewCanaryProjectTheme {
                // on below line we are specifying 
                // background color for our application
                Surface(
                    // on below line we are specifying 
                    // modifier and color for our app
                    modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background
                ) {
  
                    // on the below line we are specifying
                    // the theme as the scaffold.
                    Scaffold(
  
                        // in scaffold we are 
                        // specifying the top bar.
                        topBar = {
  
                            // inside top bar we are specifying
                            // background color.
                            TopAppBar(backgroundColor = greenColor,
  
                                // along with that we are specifying
                                // title for our top bar.
                                title = {
  
                                    // in the top bar we are 
                                    // specifying tile as a text
                                    Text(
                                        // on below line we are specifying
                                        // text to display in top app bar.
                                        text = "GFG",
  
                                        // on below line we are specifying
                                        // modifier to fill max width.
                                        modifier = Modifier.fillMaxWidth(),
  
                                        // on below line we are specifying
                                        // text alignment.
                                        textAlign = TextAlign.Center,
  
                                        // on below line we are specifying
                                        // color for our text.
                                        color = Color.White
                                    )
                                })
                        }) {
                        // on below line we are calling 
                        // method to display UI
                        ratingDialog(LocalContext.current)
                    }
                }
            }
        }
    }
}
  
@Composable
fun ratingDialog(context: Context) {
  
    // on below line creating variable for rating dialog.
    var easyRatingDialog = EasyRatingDialog(context);
  
    Column(
        modifier = Modifier
            .fillMaxHeight()
            .fillMaxWidth()
            .background(Color.White)
    ) {
        // on below line we are adding a spacer.
        Spacer(modifier = Modifier.height(120.dp))
        // on below line we are adding a text
        Text(
            // on below line specifying text for heading.
            text = "Rate Us Dialog in Android",
            // adding text alignment,
            textAlign = TextAlign.Center,
            // on below line adding text color.
            color = greenColor,
            // on below line adding font weight.
            fontWeight = FontWeight.Bold,
            // on below line adding padding from all sides.
            modifier = Modifier
                .padding(10.dp)
                .fillMaxWidth()
        )
        // on below line adding a spacer.
        Spacer(modifier = Modifier.height(50.dp))
  
        // on below line adding a button to
        // display an alerter dialog.
        Button(
            onClick = {
                // on below line creating an alerter.
                easyRatingDialog.showAnyway()
            },
            // on below line adding modifier for button.
            modifier = Modifier
                .fillMaxWidth()
                .padding(20.dp)
        ) {
            // on below line displaying text for our button.
            Text(text = "Display Rate Me Dialog", color = Color.White)
        }
  
    }
}


Now run your application to see the output of it. 

Output:



Last Updated : 20 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads