Alert dialogs are used to prompt the user to ask for a decision or some information. For example: Sometimes you might want to ask from the user for a response that will take them to another event. So, this can be done in Android development with the help of AlertDialogBuilder. Let’s understand how you can implement alert dialogs in Android with the help of an example:
Step – 1: First you need to create an Android Application in your Android studio under a package. Here, we are going to use our new package:
com.firstapp.ashulakhwan.greatlearning
Step – 2: Now, you need to modify the MainActivity.java file. In this file, you need to add the alert dialog code. You can also refer to the code below:
package com.firstapp.ashulakhwan.greatlearning;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate (Bundle savedInstanceState ) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main );
}
public void open ( View view ) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder ( this );
alertDialogBuilder.setMessage ("Choose Yes, if you want to open another window and No if you want to stay on this window.");
alertDialogBuilder.setPositiveButton ("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this,"Now, you will be taken to another window in this app.",Toast.LENGTH_LONG).show();
}
});
alertDialogBuilder.setNegativeButton ("No",new DialogInterface.OnClickListener() {
Override
public void onClick (DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create ();
alertDialog.show();
}
}
Step – 3: After modifying the MainActivity.java file, you are required to modify another file activity_main.xml where may add any GUI component. The code snippet is given below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:paddingLeft = "@dimen/activity_horizontal_margin"
android:paddingRight = "@dimen/activity_horizontal_margin"
android:paddingTop = "@dimen/activity_vertical_margin"
android:paddingBottom = "@dimen/activity_vertical_margin"
tools:context = ".MainActivity">
<TextView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Great Learning"
android:id = "@+id/textView2"
android:textColor = "#FF5733"
android:textSize = "40dp"
android:layout_below = "@+id/textView"
android:layout_centerHorizontal = "true" />
<ImageView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:id = "@+id/imageView"
android:layout_below = "@+id/textView2"
android:layout_alignRight = "@+id/textView2"
android:layout_alignEnd = "@+id/textView2"
android:layout_alignLeft = "@+id/textView"
android:layout_alignStart = "@+id/textView" />
<Button
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Alert dialog"
android:id = "@+id/button"
android:layout_below = "@+id/imageView"
android:layout_alignRight = "@+id/textView2"
android:layout_alignEnd = "@+id/textView2"
android:layout_marginTop = "50dp"
android:onClick = "open"
android:layout_alignLeft = "@+id/imageView"
android:layout_alignStart = "@+id/imageView" />
</RelativeLayout>
Step – 4: At last, you have to run the application on Android Emulator or an android device.
Now, if you have followed all the steps above, you can run the application and see how it works.