Where to put code ?
Select your Method(code) and go to MainActivity.java
paste your code there.(see below)
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);}//paste your method(code) here}
Method 1 :
A)
//exit confirmation Dialog method 1
@Override public void onBackPressed() { new AlertDialog.Builder(this) .setMessage("Are you sure you want to exit?") .setCancelable(true) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { MainActivity.this.finish(); } }) .setNegativeButton("No", null) .show(); }//exit confirmation dialog method 1
B)
If you are using below code you have to add you package name in it.
//confirm exit dialog startpublic static final String PACKAGE_NAME = "yourPackageName";
public void rate()
{
new AlertDialog.Builder(MainActivity.this)
.setMessage("Rate Us:")
.setCancelable(true)
.setNegativeButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + PACKAGE_NAME)));
}}).setNeutralButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) {
finish();
}}).show();
}
public void onBackPressed()
{
rate();
}
//confirm exit dialog end
Method 2:
In this method you can add rate us link also.App logo and App name automatically take this code.There is a small string code in here.So firstly add that string code.
open values folder and string.xml
add below yellow colored code.(you can change the message).
resources>
string name="app_name">Testing Android Appstring>string name="quit_message">Please rate us before exit...string> resources>
Then go to MainActivity.java like Method 1.
then paste below code there...
//confirm exit dialog start@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle(getString(R.string.app_name));
alert.setIcon(R.mipmap.ic_launcher);
alert.setMessage(R.string.quit_message);
alert.setNegativeButton("Exit",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
//this part optional-start
alert.setNeutralButton("No Exit",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
this part optional-end//
alert.setPositiveButton("Rate Us",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String appName = getPackageName();
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appName)));
} catch (android.content.ActivityNotFoundException e) {
startActivity(new Intent(
Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id=" + appName)
));
}
}
});
alert.show();
return true;
}
return super.onKeyDown(keyCode, event);
}
//confirm exit dialog end
Fine.That's all.please share this valuable android app exit confirmation dialog post.