For some reason setError for EditText in dialog is set, but the field is not highlighted. Dialog is loaded as separate view.
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:paddingLeft="16dp"
android:id="@+id/pwdtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pwd_text"
android:textSize="16dp"
android:layout_centerHorizontal="true"/>
<EditText
android:paddingLeft="16dp"
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/pwdtext"
android:hint="@string/login">
<requestFocus/>
</EditText>
<EditText
android:paddingLeft="16dp"
android:id="@+id/pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/login"
android:hint="@string/pwd"/>
</RelativeLayout>
And here goes invokation of dialog. Onclick listener works well. Via debugger have double-checked and getError()
for both fields returns correct values but none of fields is highlighted. Dialog is also closed but I guess this shouldn't be while field has error:
private void showAlert(){
LayoutInflater inflater = LayoutInflater.from(context);
final View promptView = inflater.inflate(R.layout.auth_prompt,null);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
dialogBuilder.setView(promptView);
dialogBuilder.setCancelable(true);
dialogBuilder.setPositiveButton("OK" , new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
EditText login = (EditText) ((AlertDialog) dialogInterface).findViewById(R.id.login);
EditText pwd = (EditText) ((AlertDialog) dialogInterface).findViewById(R.id.pwd);
//For simultaneous validation
List<EditText> fields = new ArrayList<>();
fields.add(login);
fields.add(pwd);
for(EditText field: fields){
//Avoiding double checks on null+""
String text = null;
try{
text = field.getText().toString().trim();
}catch (RuntimeException e){
text="";
}
if("".equals(text)){
field.setError("Please fill in the field");
((AlertDialog) dialogInterface).show();
}
}
}
});
dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i) {
LOG.info("CANCEL Clicked");
dialogInterface.cancel();
}
});
AlertDialog dialog = dialogBuilder.create();
dialog.show();
}
Would appreciate any help a lot.