Steven Marshall Steven Marshall - 3 months ago 33
Android Question

Confirm dialog before exit android studio

Hi guys I have an exit button on my main activity XML however I need it so when the user clicks it, it opens a dialogue to confirm the exit, if yes then exit. if no take them back.

here's my code so far, don't have any of that part however as I don't know where to start.

package com.tradingsoftwarelimited.labelprinter10;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);




//Sets Edit and Data Button to hidden
Button hiddenButton = (Button) findViewById(R.id.editButton);
hiddenButton.setVisibility(View.INVISIBLE);
Button hiddenButton1 = (Button) findViewById(R.id.dataButton);
hiddenButton1.setVisibility(View.INVISIBLE);

//Acknolages settings button
Button showButton = (Button) findViewById(R.id.settingsButton);

//if clicked
showButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//If clicked show Edit and data Button
Button showButton = (Button) (findViewById(R.id.editButton));
showButton.setVisibility(View.VISIBLE);
Button showButton1 = (Button) (findViewById(R.id.dataButton));
showButton1.setVisibility(View.VISIBLE);


}
});

}




public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getSupportActionBar().hide();
return true;
}
};


;

Answer
 private void confirmDialog(Context context){ 

   final AlertDialog alert = new AlertDialog.Builder(
                    new ContextThemeWrapper(context,android.R.style.Theme_Dialog))
                    .create();
            alert.setTitle("Alert");
            alert.setMessage("Do you want to exit ?");
            alert.setIcon(R.drawable.warning_icon);
            alert.setCancelable(false);
            alert.setCanceledOnTouchOutside(false);

            alert.setButton(DialogInterface.BUTTON_POSITIVE, "Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                            alert.dismiss();

                            finish();

                        }
                    });

            alert.setButton(DialogInterface.BUTTON_NEGATIVE, "No",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                            alert.dismiss();

                        }
                    });

            alert.show();
}

Call this method in

@Override
    public void onBackPressed() {
        super.onBackPressed();
        confirmDialog(getApplicationContext());
    } 

of your Activity

Comments