Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Different exit confirmation dialogs(eg:Rate us) in Android Studio

Without exit confirmation dialog,android app does not looks like Official one.So lets add exit confirmation dialog windows to Android Studio app.Someone like to rate us link and someone don't.Don't worry we have different codes.Let's see those.

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.


This post first appeared on GleanPost, please read the originial post: here

Share the post

Different exit confirmation dialogs(eg:Rate us) in Android Studio

×

Subscribe to Gleanpost

Get updates delivered right to your inbox!

Thank you for your subscription

×