Android Studio : AlertDialog to get user attention example
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.
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>
Follow by the Java code to create the AlertDialog box and tie it to the alert button.
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.
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
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
Tutorials
+7.8k Golang : Generate human readable password
+19.2k Mac OSX : Homebrew and Golang
+9.7k Golang : Quadratic example
+6.5k Golang : Calculate diameter, circumference, area, sphere surface and volume
+11.1k How to test Facebook App on localhost ?
+13k Swift : Convert (cast) Int to String ?
+11.4k Golang : Fix fmt.Scanf() on Windows will scan input twice problem
+5.9k Golang : List all packages and search for certain package
+11.8k Golang : Secure file deletion with wipe example
+13.7k Golang : Set image canvas or background to transparent
+11.5k Golang : Concatenate (combine) buffer data example
+16k Golang : Get file permission