Huppo
Huppo

Reputation: 971

How to get value EditText from customDialog

My scenario, there is a button in the main screen, when user click the button, a dialog form comes out with a textedit and 2 button. My problem is when I try to get the value from edit text it seems nothing happen, and the value is always NULL.

This is my code: I declare dialog inside the main activity

private void popup() {
        AlertDialog.Builder builder;
        AlertDialog alertDialog;

        LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
        View dialog = inflater.inflate(R.layout.isbn_dialog,
                                       (ViewGroup) findViewById(R.id.layout_root));

                    // The edit text from dialog
        isbnInput = (EditText)dialog.findViewById(R.id.isbn);

        builder = new AlertDialog.Builder(this);
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), isbnInput.getText().toString(), Toast.LENGTH_LONG);
            }
        });

        builder.setView(dialog);
        alertDialog = builder.create();
        alertDialog.setTitle("Enter ISBN Number");
        alertDialog.show();
}

So how can I get the value from dialog edit text properly ?

Upvotes: 0

Views: 349

Answers (2)

Tai Tran
Tai Tran

Reputation: 1406

I didnot see your editext reference with AlertDialog.

Try with this, the same as you, and the sure thing is that I can get editTextproperly

    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setMessage(getResources().getString(R.string.my_email));
    final EditText input = new EditText(this);



    final LinearLayout layout = new LinearLayout(this);
    LayoutParams params = new LayoutParams(
            (int) getResources().getDimension(R.dimen.simple_alert_width_normal),
            LayoutParams.WRAP_CONTENT);
    params.setMargins(10, 0, 10, 0);
    layout.addView(input, params);

    alert.setView(layout);
    alert.setPositiveButton(R.string.OK,
            new DialogInterface.OnClickListener() {
            // DO some thing here

            });

    alert.setNegativeButton(R.string.cancel,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // Canceled.

                }
            });

    alert.show();

Upvotes: 0

user370305
user370305

Reputation: 109237

This line must be,

 Toast.makeText(getApplicationContext(), isbnInput.getText().toString(), Toast.LENGTH_LONG).show();

You forget to show() Toast inside button's click..

Try it..

Upvotes: 1

Related Questions