Interviews7 answers
How to create alerts in android
7 Answers
http://stackoverflow.com/questions/12600360/how-to-make-an-alert-dialog-box-in-android
We need to use the AlertDialog package to create the Alert in Android
the following program demonstartes how we can create a alert dialog box
import android.content.DialogInterface;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}// Declare the onBackPressed method// when the back button is pressed// this method will call@Overridepublic void onBackPressed(){// Create the object of// AlertDialog Builder classAlertDialog.Builder builder= new AlertDialog.Builder(MainActivity.this);// Set the message show for the Alert timebuilder.setMessage("Do you want to exit ?");// Set Alert Titlebuilder.setTitle("Alert !");// Set Cancelable false// for when the user clicks on the outside// the Dialog Box then it will remain showbuilder.setCancelable(false);// Set the positive button with yes name// OnClickListener method is use of// DialogInterface interface.builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which){// When the user click yes button// then app will closefinish();}});// Set the Negative button with No name// OnClickListener method is use// of DialogInterface interface.builder.setNegativeButton("No",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which){// If user click no// then dialog box is canceled.dialog.cancel();}});// Create the Alert dialogAlertDialog alertDialog = builder.create();// Show the Alert Dialog boxalertDialog.show();}}
the above program will generte the an alert when we try to exit the app
Rohit GuptaPosted Jul 15, 2019, 1:52 AM EDT
Dear,
If You want to create real time alert then GCM (Google Cloud Messaging ) is best for Alert and Notification on device.
If you want to local alert like data pack off Alert then Alert dialog and Toast is the way by which you can alert the user
MD REHAN FAISAlPosted Sep 5, 2016, 6:05 AM EDT
Android Tutorial- http://www.hub4tech.com/android-tutorial
Simran KaurPosted Jun 23, 2016, 3:10 AM EDT
http://stackoverflow.com/questions/26097513/android-simple-alert-dialog
Neeraj KumarPosted Jun 7, 2015, 10:35 PM EDT
http://stackoverflow.com/questions/12600360/how-to-make-an-alert-dialog-box-in-android
Munesh SharmaPosted Apr 22, 2014, 3:30 AM EDT
http://www.wikihow.com/Show-Alert-Dialog-in-Android
Khan Abrar AhmedPosted Apr 6, 2014, 3:55 AM EDT





