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
+27k PHP : Count number of JSON items/objects
+17.6k Golang : Convert IPv4 address to decimal number(base 10) or integer
+9.5k Golang : Check if user agent is a robot or crawler example
+4.6k Nginx and PageSpeed build from source CentOS example
+13.3k Golang : Check if an integer is negative or positive
+24.6k Golang : convert rune to integer value
+5.4k Unix/Linux : How to test user agents blocked successfully ?
+14.8k Golang : How to get Unix file descriptor for console and file
+8k Golang : Implementing class(object-oriented programming style)
+9.6k Golang : Channels and buffered channels examples
+18.5k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+21.6k Golang : How to run Golang application such as web server in the background or as daemon?