Reputation: 23
I made a custom alert dialogue box that gets invoked by teacherLogin() method. Dialogue consists of an Editext(whose value gets stored in "text" String) and two buttons i.e. "Login" & "Cancel". When pressed Login button it shows null value of String "text". Please help iff possible??
public void teacherLogin(View view)
{
View alertview = getLayoutInflater().inflate(R.layout.custom_alert,null);
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setTitle("Login");
builder.setView(alertview);
final String text = ((EditText) alertview.findViewById(R.id.date_text)).getText().toString();
builder.setPositiveButton("Login", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
Toast.makeText(getApplicationContext(), "name:"+text, Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
Snackbar.make(findViewById(android.R.id.content), "Dialogue Cancelled", Snackbar.LENGTH_LONG)
.show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
Upvotes: 0
Views: 28
Reputation: 2076
This is because you initialize text
variable before user presses Login
button of the alert dialogue. Initialize it inside listener of positive Login
button.
private void showDialog() {
View alertview = getLayoutInflater().inflate(R.layout.custom_alert, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Login");
builder.setView(alertview);
final EditText textEditText = ((EditText) alertview.findViewById(R.id.date_text));
builder.setPositiveButton("Login", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String textValue = textEditText.getText().toString();
if(textValue!=null && !textValue.isEmpty(){
Toast.makeText(getApplicationContext(), "name:" +textValue, Toast.LENGTH_SHORT).show();
}
//show empty value toast to user
else{
Toast.makeText(getApplicationContext(), "Please enter name", Toast.LENGTH_SHORT).show();
//dismiss dialog
dialogInterface.dismiss();
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "cancelled", Toast.LENGTH_SHORT).show();
//dismiss dialog
dialogInterface.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
Upvotes: 0
Reputation: 86
Simply because you are getting the text from the EditText
before the dialogue is shown.
This works:
private void showDialog() {
View alertview = getLayoutInflater().inflate(R.layout.custom_alert, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Login");
builder.setView(alertview);
final EditText editText = ((EditText) alertview.findViewById(R.id.date_text));
builder.setPositiveButton("Login", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getApplicationContext(), "name:" + editText.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "cancelled", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
Upvotes: 2