title: Confirm dialog before exit android studio
body: <p>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.</p>
  
  <p>here's my code so far, don't have any of that part however as I don't know where to start. </p>
  
  <pre><code>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;
          }
      };
  
  
  ;
  </code></pre>