3

I want to create a custom dialog. So i create a template 'dialog_change' and I open the dialog.

Dialog myDialog = new Dialog(Overview.this);
myDialog.setContentView(R.layout.dialog_change);
myDialog.setTitle("My Custom Dialog Title");
myDialog.show();

enter image description here

Now i want to add two button (one positive and one negative button), at the bottom. How can i do that?

David
  • 4,027
  • 10
  • 50
  • 102
  • I'd first suggest trying setPositiveButton() and setNegativeButton(). – Code-Apprentice Feb 09 '13 at 00:12
  • 1
    @Code-Guru Those methods don't exist in the Dialog class. David, Why not just use an AlertDialog rather than recreate it? – Sam Feb 09 '13 at 00:24
  • @Sam Sorry, I should have double-checked the API Guides first. You could use an AlertDialog instead of a normal dialog. It has a `setButton()` method that should do what you want. – Code-Apprentice Feb 09 '13 at 00:30
  • 1
    @Sam Or better yet, use AlertDialog.Builder which *does* have setPositiveButton() and setNegativeButton() methods (as well as a setView() method for your custom view). – Code-Apprentice Feb 09 '13 at 00:32
  • @Code-Guru I agree with using an AlertDialog since this already has the Buttons and can accept a custom layout. – Sam Feb 09 '13 at 00:42
  • See my answer on same question. http://stackoverflow.com/questions/14472991/how-to-put-two-buttons-on-alert-box/14473930#14473930 – Fahad Ishaque Feb 09 '13 at 08:36

2 Answers2

5

I'd just make your own custom class to simulate an AlertDialog, this way you can use your own layout with no strings attached. (There are some weird issues where you can't fully get rid of the frame if you want a fully styled AlertDialog). Something like this, but you can expand this as fully as you want:

public class CustomDialog extends Dialog {
    private Button positive, negative;

    public CustomDialog(Context context) {
        super(context);
        initialize(context);
    }

    protected CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        initialize(context);
    }

    public CustomDialog(Context context, int theme) {
        super(context, theme);
        initialize(context);
    }

    private void initialize(Context c) {
        //Inflate your layout, get a handle for the buttons

        positive = (Button)layout.findViewById(R.id.positive):
        negative = (Button)layout.findViewById(R.id.negative):

        positive.setVisibility(View.GONE);
        negative.setVisibility(View.GONE);
    }

    public void setPositiveButton(String buttonText, View.OnClickListener listener) {
        positive.setText(buttonText);
        positive.setOnClickListener(listener);
        positive.setVisibility(View.VISIBLE);
    }

    public void setNegativeButton(String buttonText, View.OnClickListener listener) {
        negative.setText(buttonText);
        negative.setOnClickListener(listener);
        negative.setVisibility(View.VISIBLE);
    }
}
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • we can't do the same for builder.setPositiveButton("OK",positiveClick ) for _Builder android.app.AlertDialog.Builder.setPositiveButton(CharSequence text, OnClickListener listener)_ – LOG_TAG Nov 18 '13 at 11:54
1

You can add the two buttons to the custom layout that you are using for dialog(i.e. dialog_change). And then you can access them after creating the dialog as follows:

Dialog myDialog = new Dialog(Overview.this);
View view = LayoutInflater.from(context).inflate(R.layout.dialog_change,null);
myDialog.setContentView(view);
myDialog.setTitle("My Custom Dialog Title");

Button button1 = (Button)view.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v){
        dialog.dismiss();
    }
});
//Similarly for the second button
myDialog.show();
andro
  • 977
  • 1
  • 10
  • 21