Eirik Fesker Eirik Fesker - 21 days ago 4
Android Question

How can i wait after a button-click for a button-click in a dialog window

I have a small userprofile sheet with a textView for example the [email] and to the right a [button] to change the address.

When i click the button a small DialogWindow with an EditText Input opens so the user can apply the value with the positive button.

but there is a annoying effect i dont know how to solve. After the EditTextDialog is created i want to toast the input value. (see the code below)
but my first button click doesnt wait for the positive button click in the dialog of course. so there is immediately 'null' toasted before an input has been entered.

Button btnChangeEmail = (Button) getView().findViewById(R.id.btn_settings_changeemail);
btnChangeEmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyDialogBuilder myDialogBuilder = new MyDialogBuilder(getString(R.string.changeemail), getActivity());
myDialogBuilder.createEditTextInputDialog();
myDialogBuilder.toastInput();
}
});


At the buttonclick i want to store the input string in the object. Actually i want to toast it afterwards or make some other calls like send it to a MySQL-DB. But i need this as flexible as possible so that i can call it
makeInputDialog().toastTheInput().andSendItToWeb()
and so on. But as already said .. there is no string at all because the first button doesnt wait for the second.

What do i need to change? Thanks in Advance.

This is my dialog Builder class:


public class MyDialogBuilder {

String caption;
String input;
Context mContext;

public MyDialogBuilder(String caption, Context mContext) {
this.caption = caption;
this.mContext = mContext;
}

MyDialogBuilder setInputValue(String value) {
this.input = value;
return this;
}

MyDialogBuilder createEditTextInputDialog() {
LayoutInflater inflater = LayoutInflater.from(mContext);
View inputDialogView = inflater.inflate(R.layout.dialog_input_edittext, null);

AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setView(inputDialogView);

final EditText etInput = (EditText) inputDialogView.findViewById(R.id.et_inputdialog);
final TextView tvCaption = (TextView) inputDialogView.findViewById(R.id.tv_inputdialog_caption);

tvCaption.setText(caption);

builder.setCancelable(true)
.setPositiveButton(R.string.apply, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setInputValue(etInput.getText().toString());
}
}).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});

AlertDialog alertDialog = builder.create();
alertDialog.show();

return this;

}

MyDialogBuilder toastInput() {
Toast.makeText(mContext, input, Toast.LENGTH_SHORT).show();
return this;
}

}




Answer Source

In a builder pattern you just build the situation, not execute it right away. You should just apply the change and check for it later:

public class MyDialogBuilder {

    String caption;
    String input;
    Context mContext;
    boolean showToast;

    public MyDialogBuilder(String caption, Context mContext) {
        this.caption = caption;
        this.mContext = mContext;
    }

    MyDialogBuilder setInputValue(String value) {
        this.input = value;
        return this;
    }

    MyDialogBuilder createEditTextInputDialog() {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View inputDialogView = inflater.inflate(R.layout.dialog_input_edittext, null);

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setView(inputDialogView);

        final EditText etInput = (EditText) inputDialogView.findViewById(R.id.et_inputdialog);
        final TextView tvCaption = (TextView) inputDialogView.findViewById(R.id.tv_inputdialog_caption);

        tvCaption.setText(caption);

        builder.setCancelable(true)
                .setPositiveButton(R.string.apply, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        setInputValue(etInput.getText().toString());

                        // Toast here
                        if (showToast) {
                            Toast.makeText(mContext, input, Toast.LENGTH_SHORT).show();
                        }
                    }
                }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        AlertDialog alertDialog = builder.create();
        alertDialog.show();

        return this;

    }

    MyDialogBuilder toastInput() {
        // Set here to show toast or not
        showToast = true;
        return this;
    }

}

So in toastInput() you make clear that a toast should be shown, and later on you check if that boolean was set to true, if so, show the toast.