James Dudley
James Dudley

Reputation: 957

Using a single Dialog code in multi classes

I have a alertdialog that I know I will be using in multi classes thoughout my app and you can tell I would rather place it in its own class and call it when nessary. Its a simple text field and ok/cancel buttons. I always want to pass some text into it and after the user has clicked OK I want a variable to be updated with the updated text string

Whats the best way at converting this code so it can be used else where

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View layout = inflater.inflate(R.layout.dialog_layout, null);

editTextfield = (EditText) layout.findViewById(R.id.comment_text);
editTextfield.setText(text);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.setTitle(R.string.title);

builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {

        text = editTextfield.getText().toString();

            removeDialog(DIALOG);

});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {

           removeDialog(DIALOG);

    }
});

AlertDialog dialog = builder.create();
return dialog;

Thanks for you time

Upvotes: 0

Views: 145

Answers (3)

Guillaume
Guillaume

Reputation: 22822

I did exactly that. Created a ConfirmDialog class that creates the AlertDialog.Builder in its constructor, and delegates the show call in its own show method. Just pass a parent parameter (the calling activity), and use it to do all your resources gathering.

Have a String variable on that class, and a getter to get it.

Then you can do something like (from any activity):

....
final ConfirmDialog myDialog = new ConfirmDialog(initialText);
myDialog.show();
....

And when you need the text

myDialog.getText();

EDIT: More details

public class ConfirmDialog {
    private final Context parent;
    private final AlertDialog dialog;    
    private String text;

    public ConfirmDialog(Context parent) {
        this.parent = parent;

        final EditText editTextfield = (EditText) layout.findViewById(R.id.comment_text);
        editTextfield.setText(text);

        this.dialog = new AlertDialog.Builder(parent)
                       .setView(LayoutInflater.from(parent).inflate(R.layout.dialog_layout, null));
                       .setTitle(R.string.title); // you can also pass the title if you want a different one each time you instanciate the dialog
                       .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                              setText(editTextfield.getText().toString());
                              removeDialog(DIALOG);
                           }
                        });
                       .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                              removeDialog(DIALOG);
                           }
                        })
                       .create();
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }

    public void show() {
        dialog.show();
    }
}

(I coded it quickly in the StackOverflow editor, so there may be syntax errors)

Upvotes: 1

rajpara
rajpara

Reputation: 5203

Read the answer in this question "Sharing-menu-bar between Activities

Even though requirement is different but solution can be used in your problem

Upvotes: 1

coder_For_Life22
coder_For_Life22

Reputation: 26981

You should check out this tutorial it shows you exactly how to do what you are trying to do. You have to extend the Dialog class.

Custom Dailog class

Upvotes: 1

Related Questions