-4

I already have an onOptionsItemSelected(MenuItem item) for a submit button.

In the

switch(item.getItemId()){
case R.id.submit:

I want an alertDialog. How may I do that. This submit is the id for the image in the res>drawable folder. So how can I add an alert dialog to this?

Summarizing: I want a confirmation for the submit button clicked. Upon a yes I want to call the submitDefects(); function.

Pramod Setlur
  • 811
  • 1
  • 15
  • 27

1 Answers1

1
public boolean onOptionsItemSelected (MenuItem item)
 {
 switch(item.getItemId()){
 case R.id.submit:
  AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

    // Setting Dialog Title
    alertDialog.setTitle("TITLE");

    // Setting Dialog Message
    alertDialog.setMessage("Are you sure you want delete this?");

    // Setting Icon to Dialog
    alertDialog.setIcon(R.drawable.delete);

    // Setting Positive "Yes" Button
    alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int which) {

        // Write your code here to invoke YES event
        Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
                     submitDefects();
        }
    });

    // Setting Negative "NO" Button
    alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        // Write your code here to invoke NO event
        Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
        dialog.cancel();
        }
    });

    // Showing Alert Message
    alertDialog.show();
            return true;
        }
    }
Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96