EditText setError() with no message just the icon
Your code perfect to show icon only.
As EditText
is show anything related to when it has focused. So just change your code to like that...
if (!emailmatcher.matches()) {
email.requestFocus();
email.setError("Invalid Email");
}
if (!zipmatcher.matches()) {
zipcode.requestFocus();
zipcode.setError("Invalid ZipCode");
}
EditText setError() with icon but without Popup message
Problem solved after a lot of research and permutations- (Also thanks to @van)
Create a new class that will extend EditText
something like this-
public class MyEditText extends EditText {
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setError(CharSequence error, Drawable icon) {
setCompoundDrawables(null, null, icon, null);
}
}
Use this class as a view in your xml like this-
<com.raj.poc.MyEditText
android:id="@+id/et_test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
Now in the third step, just set a TextWatcher
to your custom text view like this-
et = (MyEditText) findViewById(R.id.et_test);
errorIcon = getResources().getDrawable(R.drawable.ic_error);
errorIcon.setBounds(new Rect(0, 0, errorIcon.getIntrinsicWidth(), errorIcon.getIntrinsicHeight()));
et.setError(null,errorIcon);
et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
if(s.toString().length()>6){
et.setError("", null);
}else{
et.setError("", errorIcon);
}
}
});
where R.drawable.ic_error
=
Keeping text null solves the problem
But if we keep only null in setError(null), this won't show the validation error; it should be null along with second param.
EditText.setError doesnt show Error text and only shows icon
Use TextInputLayout
for material EditText
like :
<android.support.design.widget.TextInputLayout
android:id="@+id/input_application_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:minHeight="50dp"
android:theme="@style/MyTextInputLayout"
app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout">
<EditText
android:id="@+id/application_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_application_name"
android:imeOptions="actionNext"
android:inputType="text" />
</android.support.design.widget.TextInputLayout>
and in your Activity
TextInputLayout til = (TextInputLayout) findViewById(R.id.input_application_name);
EditText applicationNameEdt = (EditText) findViewById(R.id.application_name);
til.setErrorEnabled(true);
til.setError("You need to enter a name");
EditText.setError()'s popup message layout looks weird
You Can use Snackbar as Validation which looks much better and have
customization as u can show proper message for specific period of
time... Example: on textChangedListener or On Button Click-->submitBT = (Button) findViewById(R.id.submitBT);
submitBT.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (loginET.getText().toString().isEmpty() || loginET.length() == 0 || loginET.length() > 50) {
Snackbar.make(view, "Please Enter Username/Email", Snackbar.LENGTH_SHORT).show();
return;
}
if (passwordET.getText().toString().isEmpty() || passwordET.length() == 0 || passwordET.length() > 50) {
Snackbar.make(view, "Password", Snackbar.LENGTH_SHORT).show();
}
}
});you can also use for textChangedListener...
And the view will not automatically scrolled up like seterror
Customizing the setError icon for EditText
This issue is discussed and resolved here:
EditText setError() with icon but without Popup message
I hope that by extending the answer it would not get auto-converted to a comment.
EditText setError is out of place in BottomSheetDialog
You can use TextInputLayout
and inside that you can define Edit Text
.
I have done some modification inside your phone_dialog.xml
.
phone_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="bottom"
android:background="@android:color/holo_blue_light"
android:padding="10dp"
app:behavior_hideable="true"
app:behavior_peekHeight="60dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:gravity="center"
android:padding="10dp"
android:text="dialog_title_edit_phone" />
<android.support.design.widget.TextInputLayout
android:id="@+id/inputPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/TextInputLayoutLabel">
<EditText
android:id="@+id/etPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter phone"
android:imeOptions="actionDone"
android:inputType="phone"
android:maxLines="1"
android:textSize="20sp" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/btnSavePhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/etPhone"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="8dp"
android:text="Save" />
</LinearLayout>
</FrameLayout>
Inside style.xml
add this.
<style name="TextInputLayoutLabel" parent="Widget.Design.TextInputLayout">
<!-- Hint color and label color in FALSE state -->
<item name="android:textColorHint">@android:color/black</item>
<item name="android:textSize">15sp</item>
<!-- Label color in TRUE state and bar color FALSE and TRUE State -->
<item name="colorAccent">@color/colorPrimary</item>
<item name="colorControlNormal">@color/colorAccent</item>
<item name="colorControlActivated">@color/colorAccent</item>
</style>
PhoneBottomDialog.java
public class PhoneBottomDialog extends BottomSheetDialog {
TextInputLayout inputPhone;
EditText edtPhone;
Button btnSave;
public PhoneBottomDialog(Context context) {
super(context);
View view = getLayoutInflater().inflate(R.layout.phone_dialog, null);
setContentView(view);
// additional setup below this...
inputPhone = (TextInputLayout) view.findViewById(R.id.inputPhone);
edtPhone = (EditText) view.findViewById(R.id.etPhone);
btnSave = (Button) view.findViewById(R.id.btnSavePhone);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!validatePhone())
return;
}
});
}
private boolean validatePhone() {
if (edtPhone.getText().toString().isEmpty()) {
inputPhone.setError("Please enter valid phone number with country code.");
requestFocus(edtPhone);
return false;
} else {
inputPhone.setErrorEnabled(false);
}
return true;
}
private void requestFocus(View view) {
if (view.requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
// ...
}
And inside Activity
.
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test24);
mContext = this;
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
PhoneBottomDialog dialog = new PhoneBottomDialog(mContext);
dialog.show();
}
Below you can see the output for this.
Related Topics
Android Detect Done Key Press for Onscreen Keyboard
Android: How to Use Webcam in Emulator
Listview in Arrayadapter Order Get's Mixed Up When Scrolling
Return Value from Asynctask Class Onpostexecute Method
How to Detect Power Connected State
Android Textview:"Do Not Concatenate Text Displayed with Settext"
Cannot Launch Avd in Emulator:Qt Library Not Found
How to Get Images Dynamically from Drawable Folder
Android Mask Bitmap on Canvas Gen a Black Space
Android: How to Get a Custom View's Height and Width
Which Android Ide Is Better - Android Studio or Eclipse
Error Loading the Sdk When Eclipse Starts
Getting MAC Address in Android 6.0
How to Send File Using Bluetooth on Android Programatically
Vertical Line Using Xml Drawable