menu_on_top
menu_on_top

Reputation: 2613

How to open a dialog when I click a button?

I have a button and I would like to open a dialog when pressed. This is my code:

Button more = (Button) findViewById(R.id.more);
more.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        //Intent myIntent = new Intent(view.getContext(), agones.class);
        //startActivityForResult(myIntent, 0);

        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("hi");
        alertDialog.setMessage("this is my app");

        alertDialog.setButton("Continue..", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // here you can add functions
        }
        });
    }
});

Upvotes: 23

Views: 101548

Answers (4)

Marci
Marci

Reputation: 969

Aman Alam's souliton is good, but the .setButton() part gave me an error. So I implemented it in kotlin and fixed the error.

Kotlin

    val dialog = AlertDialog.Builder(this)

    dialog.setTitle("Title")
          .setMessage("Write your message here.")
          .setPositiveButton("YES") { dialog, whichButton ->
               // DO YOUR STAFF
          }
         .setNegativeButton("NO") { dialog, whichButton ->
               // DO YOUR STAFF 
               // dialog.close()
          }

    dialog.show()

The result of the code

More about dialogs: https://developer.android.com/guide/topics/ui/dialogs

Upvotes: 1

Shubham Sharma
Shubham Sharma

Reputation: 1

            final AlertDialog.Builder builder = new AlertDialog.Builder(this);

            builder.setMessage("this is message");
            builder.setTitle("this is title");

            //Setting message manually and performing action on button click
            builder.setMessage("Do you want to close this application ?");T
            //This will not allow to close dialogbox until user selects an option
            builder.setCancelable(false);
            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            Toast.makeText(this, "positive button", Toast.LENGTH_SHORT).show();
                            //builder.finish();
                        }
                    });
             builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            //  Action for 'NO' Button
                            Toast.makeText(this, "negative button", Toast.LENGTH_SHORT).show();
                            dialog.cancel();
                        }
                    });

            //Creating dialog box
            AlertDialog alert = builder.create();
            //Setting the title manually
            //alert.setTitle("AlertDialogExample");
            alert.show();

Upvotes: -2

Aman Aalam
Aman Aalam

Reputation: 11251

As @Roflcoptr has said, you haven't called alertDialog.show() method. thus your dialog doesn't appear.

Here's your edited code:

Button more = (Button) findViewById(R.id.more);
more.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        //Intent myIntent = new Intent(view.getContext(), agones.class);
        //startActivityForResult(myIntent, 0);


        AlertDialog alertDialog = new AlertDialog.Builder(<YourActivityName>this).create(); //Read Update
        alertDialog.setTitle("hi");
        alertDialog.setMessage("this is my app");

        alertDialog.setButton("Continue..", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int which) {
              // here you can add functions
           }
        });

        alertDialog.show();  //<-- See This!
    }

});

if you write this instead of <ActivityName>.this, then it is going to take the reference of View.OnClickListener since this is currently being accessed inside it. You need to give your Activity's name there.

Upvotes: 43

RoflcoptrException
RoflcoptrException

Reputation: 52249

Your dialog isn't displayed, because you don't call AlertDialog#show.

Upvotes: 11

Related Questions