
Aa the name suggest Alert means it will show a alert message to user before proceeding to take any action using a small winodws known as Android Dialog and hence the name Android Alert Dialog. You can create either default alert dialog or also can make your own custom alert dialog. Click here to Know about Custom Alert Dialog in Android Studio.
Creating an Alert will include three things such as: A Title, Content Area and Action buttons as showing below:

Alert Dialog Parts:
Title:- It is the name of your content or dialog which is related to. It is optional to set title.
Content Area:- This incude the message or information which you want to show to the users. In the content area you can use anything like a button, slider, image, textview, Radio Button, Checkbox, List, EditText etc
Action Buttons:- An Alert dialog consist of maximum only three button such as: Positive, Negative and Neutral. Postive button will act like an OK button means it will use to accept and continue the app. And a Negatiev Button is used to cancel the dialog or message or refuse anything. And Neutral button is used when the user Dont want to accept (Positve) or Refuse (Negative) then you can use third button as Remind me Later which will show the dialog after some time.
How to add alert in app:
To add Android Alert Dialog you should use “AlertDialog.Builder” in your MainActivity.java file as:
AlertDialog.Builder myDialog;
Then add a button to your layout file to call or show the dialog:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:onClick="ExitApp" android:padding="5dp" android:background="@drawable/gradeint" android:textColor="@color/white" android:text="Click Here to Exit "/> </LinearLayout>

Now you can copy the below code to make a alert dialog every line is commented which helps you to learn quickly:
import android.annotation.SuppressLint; import android.app.Activity; import android.content.DialogInterface; import android.graphics.Color; import com.your.packagename.R; import android.os.Build; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.widget.RadioButton; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AlertDialog; public class MainActivity extends Activity { AlertDialog.Builder myDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myDialog = new AlertDialog.Builder(this); // this is for Activity // myDialog = new AlertDialog.Builder(getActivity()); // this is for fragment, to use fragment just comment above line and comment out this line } public void showToast (String message){ Toast toast = Toast.makeText(this, message, Toast.LENGTH_LONG); View tv = toast.getView(); tv.setPadding(20, 15, 20, 15); TextView toastMessage = (TextView) tv.findViewById(android.R.id.message); toastMessage.setTextSize(25); toastMessage.setTextColor(Color.WHITE); toastMessage.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_play, 0, 0, 0); toastMessage.setGravity(Gravity.CENTER); toastMessage.setCompoundDrawablePadding(16); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { tv.setBackground(getDrawable(R.drawable.gradeint)); } toast.show(); } public void ExitApp(View view) { myDialog.setTitle("App Close Dialog"); // dialog title myDialog.setMessage("Are you sure you want to close the app?") // dialog message or content are .setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { // action button positive @Override public void onClick(DialogInterface dialogInterface, int i) { finish(); // close the app } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { // action button negative @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.cancel(); // cancel the dialog } }) .setNeutralButton("Remid Later", new DialogInterface.OnClickListener() { // action button neutral @Override public void onClick(DialogInterface dialogInterface, int i) { showToast("OK Remind you later"); dialogInterface.cancel(); } }); AlertDialog alertDialog = myDialog.create(); // Create alert dialog box with dialog window alertDialog.show(); // finally show the alert dialog } }
below screenshot showing how you dialod will appeare:

Leave a Comment
You must be logged in to post a comment.