Android Studio : AlertDialog to get user attention example



Tags : android-studio alert-dialog empty-activity toast java

Android has dialog boxes to get user input and one of them is AlertDialog. For this tutorial, let's learn how to get user selection via the AlertDialog box. An AlertDialog box is useful in the event that your Android application needs to know what to do next or get the user the attention, hence the word AlertDialog. In the event that the user want to quit the application, you may want to prompt the user with an AlertDialog box to confirm it is really bye-bye or the user accidentally pressed the quit button.

We will learn how to build a simple AlertDialog shown below with AlertDialog.Builder. The AlertDialog we are going to create has 3 functions, one to close the application, one to close the dialog box itself and the last one to display a toast message.

Our final AlertDialog app with 3 functions

First, let's design our app. Place a button in the middle such as show below.

The text for the design:


 <?xml version="1.0" encoding="utf-8"?>
 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.example.sweetlogic.alertdialog.MainAlertDialog"
 tools:layout_editor_absoluteX="0dp"
 tools:layout_editor_absoluteY="81dp">

 <Button
 android:id="@+id/buttonAlertDialog"
 android:layout_width="300dp"
 android:layout_height="50dp"
 android:text="Alert Button"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintLeft_toLeftOf="parent"
 app:layout_constraintRight_toRightOf="parent"
 app:layout_constraintTop_toTopOf="parent"
 tools:layout_editor_absoluteX="42dp"
 tools:layout_editor_absoluteY="230dp" />

 </android.support.constraint.ConstraintLayout>

layout designer alert button

Follow by the Java code to create the AlertDialog box and tie it to the alert button.

AlertDialog box Java codes

The Java codes to create the AlertDialog box:


 package com.example.sweetlogic.alertdialog;

 import android.content.Context;
 import android.content.DialogInterface;
 import android.os.Bundle;
 import android.support.v7.app.AlertDialog;
 import android.support.v7.app.AppCompatActivity;
 import android.view.View;
 import android.widget.Button;
 import android.widget.Toast;

 public class MainAlertDialog extends AppCompatActivity {


 final Context context = this;
 private Button button;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main_alert_dialog);

 addListenerOnButton();
 }

 public void addListenerOnButton() {
 Button button = (Button) findViewById(R.id.buttonAlertDialog);

 button.setOnClickListener(new View.OnClickListener() {

 @Override
 public void onClick(View arg0) {

 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

 alertDialogBuilder.setTitle("Alert! Make a selection!");

 alertDialogBuilder.setMessage("Close app, close dialog or Toast!");

 // show a toast message
 alertDialogBuilder.setNeutralButton("Toast", new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog, int id) {
 Toast.makeText(MainAlertDialog.this, "This is a Toast Message!", Toast.LENGTH_SHORT).show();
 }
 });

 // close the AlertDialog box
 alertDialogBuilder.setNegativeButton("Close dialog", new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog, int id) {
 dialog.cancel();
 }
 });

 // close the application
 alertDialogBuilder.setPositiveButton("Close app",new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog, int id) {
 MainAlertDialog.this.finish();
 }
 });

 // all set and time to build and show up!
 AlertDialog alertDialog = alertDialogBuilder.create();
 alertDialog.show();


 }
 });

 }
 }

and hit the Run button. If everything goes well, your app should appear in the AVD/emulator.

Our final AlertDialog app with 3 functions

Happy coding!

References:

https://www.socketloop.com/tutorials/android-studio-image-button-and-button-example

https://developer.android.com/reference/android/app/AlertDialog.html

  See also : Android Studio : Image button and button example



Tags : android-studio alert-dialog empty-activity toast java

By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.


Advertisement