Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How do I retrieve Edittext.getText() in custom AlertDialog?

The topic explains what i'm after... I can't retrieve the EditText from my custom view in android. All I get is a Nullpointer Exception. :/ I've marked where the problems are in the code with comments. The ID:s are correct and my XML layout is a simple RelativeLayout containing two EditText attributes. Obviously I'm missing something trivial here, but I've now stared at the code for almost 2 hours without solving this, so I thought I'll give SO a try instead.

protected void showLoginDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    // Get the layout inflater
    LayoutInflater inflater = this.getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.activity_login, null))
    // Add action buttons
    .setPositiveButton(R.string.login, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {

            /* ERROR HERE! */
            EditText uName, passWord;
            uName = (EditText) findViewById(R.id.login_username);
            passWord = (EditText) findViewById(R.id.login_password);

            Log.i(TAG, uName.getText().toString() + " " + passWord.getText().toString());
            /* STOP */

            if(the_view.getSocketTask().isConnected) {
                the_view.getSocketTask().send_command("LOGIN ");
            } else {
                showToast("Not connected!");
            }
        }
    })
    .setNegativeButton(R.string.cancel,  new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
        }
    });      
    builder.create().show();
}

EDIT:

After suggestions the following code is a working one! Thanks again!

protected void showLoginDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    // Get the layout inflater
    LayoutInflater inflater = this.getLayoutInflater();



    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.activity_login, null))
    // Add action buttons
    .setPositiveButton(R.string.login, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            Dialog f = (Dialog) dialog;
            /* ERROR HERE! */
            EditText uName, passWord;
            uName = (EditText) f.findViewById(R.id.login_username);
            passWord = (EditText) f.findViewById(R.id.login_password);

            Log.i(TAG, uName.getText().toString() + " " + passWord.getText().toString());
            /* STOP */

            if(the_view.getSocketTask().isConnected) {
                the_view.getSocketTask().send_command("LOGIN ");
            } else {
                showToast("Not connected!");
            }
        }
    })
    .setNegativeButton(R.string.cancel,  new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
        }
    });      
    builder.create().show();
}

Thanks in advance! Alex

like image 986
Alexander W Avatar asked Oct 09 '12 12:10

Alexander W


3 Answers

Cast the Dialog as a View:

View v_iew=inflater.inflate(R.layout.activity_login, null)) ;
builder.setView(v_iew);

Then replace:

        uName = (EditText) findViewById(R.id.login_username); 
        passWord = (EditText) findViewById(R.id.login_password);

with

        uName = (EditText) v_iew.findViewById(R.id.login_username);
         passWord = (EditText) v_iew.findViewById(R.id.login_password); 
like image 158
Mohamed_AbdAllah Avatar answered Nov 10 '22 12:11

Mohamed_AbdAllah


(Can't vote so creating new answer: Took me hours to figure this out; adding the qualifier finally fixed it - like you figure out already)

Doesnt work: final Dialog dialog = new Dialog(this); ... keyInput = (EditText) findViewById(R.id.key_input);

Works: final Dialog dialog = new Dialog(this); ... keyInput = (EditText) dialog.findViewById(R.id.key_input);

like image 31
Kevin Avatar answered Nov 10 '22 11:11

Kevin


In the case of an EditText you should implement TextWatcher It is generally a very bad idea to use editText.getText()

Here is a very simple example code for Custom Dialog containing an EditText as part of the layout. There is also a button that needs to be on the same layout which when clicked will show you the text which you just entered. Have fun!

        final String inputString = null;
        final Dialog dialog = new Dialog(YourActivityName.this);
        dialog.setContentView(R.layout.custom_dialog_layout);
        EditText editText = (EditText) dialog.findViewById(R.id.id_of_edit_text);
        Button done = (Button) dialog.findViewById(R.id.done);
        dialog.show();
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                inputString = s.toString();

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });

        done.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(YourActivityName.this, inputString, Toast.LENGTH_SHORT);
                dialog.dismiss();

            }
        });
like image 1
Rajat Anantharam Avatar answered Nov 10 '22 11:11

Rajat Anantharam