Android Jetpack Compose – Display Popup Message using Alerter
In many applications, we get to see that app displays the notification when it is running on the top app bar. In this article, we will take a look at How to implement that alert notification in android applications using the Alerter library using Jetpack Compose. We will be building a simple application and displaying a button in it. On clicking on that button we will be displaying that alert notification. 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 a dependency in build.gradle
Navigate to app>build.gradle and add the below dependency to it in the dependencies section.
implementation 'com.github.tapadoo:alerter:7.2.4'
After that navigate to settings.gradle file and add the below line in the repositories section to add the maven url.
maven { url "https://jitpack.io" }
After adding the above code simply sync your project to install the dependency.
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.ActivityNotFoundException import android.content.Context import android.content.Intent import android.net.Uri import android.os.Bundle import android.util.Log import android.widget.Button import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.appcompat.app.AppCompatDelegate import androidx.compose.foundation.background import androidx.compose.foundation.layout.* import androidx.compose.material.* import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color.Companion.Red import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalInspectionMode import androidx.compose.ui.text.TextStyle 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.compose.ui.viewinterop.AndroidView import androidx.core.content.ContextCompat.startActivity import com.example.newcanaryproject.ui.theme.NewCanaryProjectTheme import com.example.newcanaryproject.ui.theme.greenColor import com.google.android.gms.ads.AdRequest import com.google.android.gms.ads.AdSize import com.google.android.gms.ads.AdView import com.tapadoo.alerter.Alerter 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 // title 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 displayAlerter(LocalContext.current) } } } } } } @Composable fun displayAlerter(context: Context) { val activity = context as Activity 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 = "Alerter 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. Alerter.create(activity) // on below line specifying title for it. .setTitle( "Welcome to Beginner for Beginner" ) // on below line specifying text for alerter. .setText( "A Computer Science Portal" ) // on below line setting icon for it. .setIcon(R.drawable.android) // on below line setting background color for it. .setBackgroundColorRes(R.color.purple_200) // on below line displaying alerter dialog. .show() }, // on below line adding modifier for button. modifier = Modifier .fillMaxWidth() .padding( 20 .dp) ) { // on below line displaying text for our button. Text(text = "Display Alerter Dialog" , color = Color.White) } } } |
Now run your application to see the output of it.
Note: Make sure to add your own image in the drawable folder to display.
Output:
Contact Us