JoeyCK JoeyCK - 15 days ago 9
Android Question

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();

amp amp
Answer

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();
Comments