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
+14.1k Golang : How to determine if user agent is a mobile device example
+6.3k Golang : Embedded or data bundling example
+18.9k Golang : Get RGBA values of each image pixel
+30.1k Get client IP Address in Go
+9k Golang : How to get garbage collection data?
+11.3k Golang : Secure file deletion with wipe example
+10.1k Android Studio : Simple input textbox and intercept key example
+3k Golang : Fix go-cron set time not working issue
+4.4k JavaScript : Rounding number to decimal formats to display currency
+13.7k Golang : Reverse IP address for reverse DNS lookup example
+14.8k Golang : How to check if IP address is in range
+11.8k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter