how to read user input from custom dialog in android?
Posted
by urobo
on Stack Overflow
See other posts from Stack Overflow
or by urobo
Published on 2010-05-23T15:04:40Z
Indexed on
2010/05/23
15:11 UTC
Read the original article
Hit count: 857
I'd like to use a custom dialog built over an AlterDialog to obtain login info from the user. In this manner I first use the layoutinflater to get the layout and then put it in the AlertDialog.Builder.setView() method.
LayoutInflater inflater = (LayoutInflater) Home.this.getSystemService(LAYOUT_INFLATER_SERVICE);
layoutLogin = inflater.inflate(R.layout.login,(ViewGroup) findViewById(R.id.rl));
My layout consists of two textview and two editext for username and password respectively. Then I override the onCreateDialog method, checking the dialog id and putting all together, during the building phase I use the setButton(...) method to add a confirmation Button, neutral though:
/* (non-Javadoc)
* @see android.app.Activity#onCreateDialog(int)
*/
@Override
protected Dialog onCreateDialog(int id) {
AlertDialog d = null;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
switch(id){
...
case Home.DIALOG_LOGIN:
builder.setView(layoutLogin);
builder.setMessage("Sign in to your DyCaPo Account").setCancelable(false);
d=builder.create();
d.setTitle("Login");
Message msg = new Message();
msg.setTarget(Home.this.handleLogin);
d.setButton(Dialog.BUTTON_NEUTRAL,"Sign in",msg);
break;
...
}
return d;
}
Then I setup the Handler handleLogin:
private Handler handleLogin= new Handler(){
/* (non-Javadoc)
* @see android.os.Handler#handleMessage(android.os.Message)
*/
@Override
public void handleMessage(Message msg) {
String input = usernameInput.getText().toString(); //this should hold the EditText field for the username
}
};
which is just a stub up to now. what I don't get is when and where I have to access the two fields since I tried to save a reference to them but unfortunately I always get a null pointer exception. Can anyone tell me what I do wrong and give some guidelines to work with custom dialogs. Thanks in advance! :)
© Stack Overflow or respective owner