Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add positive button to Dialog

I have a very simple custom dialog and I wan't to add a positive button without having to modify the XML file, just like you would do it with an AlertDialog but I don't know if it's possible. This is the code:

final Dialog dialog = new Dialog(MyActivity.this);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("Settings");
dialog.show();
like image 379
JoeyCK Avatar asked May 22 '12 22:05

JoeyCK


People also ask

How to add button in dialog android studio?

setPositiveButton(String, DialogInterface. OnClickListener) to set up your buttons. Finally, use AlertDialog myAlertDialog = myAlertDialogBuilder. create() to get your instance of AlertDialog, which you can then further customize with methods such as setCancelable() .

What is use of neutral button in dialog box?

When user click yes button it will show yes message, no button shows no message and neutral button for neutral message. In the above result, it shown initial screen. Now click on button it will open dialog with yes, no, and neutral buttons.

How many buttons can an alert dialog display?

Alert Dialog allows maximum three action buttons in a dialog.


1 Answers

You should use the builder.

LayoutInflater inflater = LayoutInflater.from(this);
View dialog_layout = inflater.inflate(R.layout.dialog,(ViewGroup) findViewById(R.id.dialog_root_layout));
AlertDialog.Builder db = new AlertDialog.Builder(MyActivity.this);
db.setView(dialog_layout);
db.setTitle("settings");
db.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
    }
});
AlertDialog dialog = db.show();
like image 187
amp Avatar answered Nov 15 '22 20:11

amp