Home » Android » Show alert dialog with options on click on Button in Android

Show alert dialog with options on click on Button in Android

Following code shows you how you can show an alert dialog with options such as Cancel, Subscribe and No .. All this options can be customised to take action as you want.

This alert has been shown on click on the contact button.

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

       buttonContact = findViewById(R.id.contatctBtn);

       buttonContact.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               final AlertDialog.Builder builder = new AlertDialog.Builder(MemberViewProfile.this);
               builder.setMessage("This is message shown on dialog").setCancelable(false).setPositiveButton("Upgrade Membership", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       Intent newIntent = new Intent(MemberViewProfile.this, UpgradeMembershipPlanActivity.class);
                       newIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                       MemberViewProfile.this.startActivity(newIntent);
                   }
               });
               builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       //do things
                   }
               });
               builder.setNeutralButton("CANCEL", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       //do things
                   }
               });
               AlertDialog alert = builder.create();
               alert.show();
           }
       });

   } //close onCreate


Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment