How to open a dialog when I click a button?
Your dialog isn't displayed, because you don't call AlertDialog#show.
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()
More about dialogs: https://developer.android.com/guide/topics/ui/dialogs
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.