Skip to main content

Android Development: Using Alert Dialog with Button in Jetpack Compose

 


Introduction:

In Android application development, buttons could be programmed to perform some actions (such as navigating to another screen, showing toast message, closing the app, among others) when the user taps or clicks on them.

In this article, we will learn how to make a button to perform the action of displaying an alert message to the user. This would be achieved using a composable function called AlertDialog. Note that we will be demonstrating this with the 'Click Here to Proceed' button located inside the ‘DemoScreen’ composable function in our 'Demo One' Android project from my previous article.

Demonstration:

First, we will open the 'Demo One' project.

‘Demo One’ project is now opened as shown in Figure 1.

Figure 1
Next, above the button composable in our ‘DemoScreen’ composable function, we will define a mutable state variable, set its value to ‘false’, and store it with the name 'showAlertMessage' (as shown in Figure 2). Note that we will be using the 'remember' keyword to remember the current value of 'false' (assigned to the variable) across recompositions. (Press ‘alt+enter’ on the remember function to import it).
Figure 2
Next, we will define the contents for our Alert Dialog. So, we will use 'if statement', pass in 'showAlertMessage' with the value which we already remembered as 'false' (as in Figure 3).
Figure 3
Next, we will define the AlertDialog function and specify the following:

§  'onDismissRequest' parameter which will hide the dialog when the user presses the back button of the device or any part of the screen outside the dialog.

§  'title' which will display the title of our alert message (for example 'CONFIRM').

§  'text' which will display our main alert message. To make the text scrollable (in case you have long statements as your alert message), we will apply 'verticalScroll' modifier with 'rememberScrollState() function. Our alert message will be “Hello User! To navigate to the next page, tap on NEXT button. Thank you.”

§  'confirmButton' which will hide the dialog (by simply setting the value of 'showAlertMessage' to false), and perform the specified action in it when tapped. We will make the background color of the button to be black, and set the text color to white, and make the font weight to be bold.

Figure 4 shows the AlertDialog composable function that we have defined.
Figure 4
Next, inside the onClick parameter of the 'Click Here to Proceed' button, we will update the value of 'showAlertMessage' to true (as in Figure 5), thereby making the Alert Dialog to be displayed.
Figure 5
That's it.

Conclusion:

When the button is clicked, the alert dialog message will now show (as shown in Figure 6). Note that when the user taps on the NEXT button, the alert dialog will close. No further action will be performed because we have not specified more actions within the onClick parameter of the NEXT button.
Figure 6

The lines of code in the MainActivity.kt of our 'Demo One' project are displayed in Figure 7 to 10 as follows:
Figure 7

Figure 8

Figure 9

Figure 10

Thank you for reading.
Happy coding!



















Comments

Popular Posts

Android Development: Addition of Bottom Navigation Bar with Kotlin and Jetpack Compose

  Introduction In this article, we will add bottom navigation bar to the second screen of the “Demo One” App that I have been using for demonstrations in my previous Android development articles. The bottom navigation bar will have three navigation items, which are, “Home”, “Info” and “Settings”.  The navigation items would be programmed to render their contents on the screen accordingly when clicked or tapped. Note that the “Demo One” Android project already had a TopAppBar, as added in my previous article. The user interface of the second screen of the Demo One App is as shown in Figure 1. Figure 1 Addition of Needed Dependency The dependency that we will need for the addition of bottom navigation bar to our Android project is called “navigation”. Let us now open the module-level build.gradle file in our Android project and add the version 2.6.0 of the navigation dependency. After the addition of the dependency, we will connect to the internet and then click on “Sync...

Simple method of hiding page number on the first page of your Microsoft Word document

    Introduction: According to the standard in preparing documents in most institutions or organizations, it is usually expected that the page number should not appear on the first or title page of a document. Hence, different methods of ensuring that the page number becomes hidden on the first page have been tried. In this article, we will consider a simple and efficient method. Any time you insert page numbers in your Microsoft Word document, the page numbers will appear on all the pages (including the first page) in your document. Page numbers could appear at the “Top of Page” (which is the header) or “Bottom of Page” (which is the footer), depending on where you have chosen to position them. Nevertheless, if the first page in your document is a “Cover Page” that was inserted from the “Pages” group in the “Insert” menu tab, then, the first page will not carry any page number by default. Figure 1 shows the first two pages of our document, and our task is to hide th...

How to change the default app icon to the preferred icon for your Android app project in Android Studio

  Introduction: Every Android mobile application usually comes with unique app icon associated with it by the Android Developer. An app icon visually represents a mobile application on the screen of user’s device, or in any app store, for example, Figure 1 shows app icons for various applications on the screen.   Figure 1 It is therefore a good idea to have an image designed for the Android application that you are building. The dimension of the app icon could be 512 x 512 pixel. You can then add the app icon to your Android project to replace the default launcher icon associated with app projects in Android Studio. Demonstration: In this demonstration, we will be replacing the default icon with our app icon (as in Figure 2).   Figure 2 To add your app icon to your Android project, take note of the following: First, ensure that your app icon image has been designed and ready for use (for example, Figure 3 shows an image named “my_app_icon”- designed for “Dem...