How to replace particular word from edittext - android
I have one edittext: edittextmysite.
Now I want to provide default text, for example: "https://wwww.mysite.com/"
I have achieved it this as follows:
edittextmysite.setText("https://wwww.mysite.com/");
Selection.setSelection(edittextmysite.getText(), edittextmysite.getText().length());
edittextmysite.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (!s.toString().contains("https://wwww.mysite.com/")) {
edittextmysite.setText("https://wwww.mysite.com/");
Selection.setSelection(edittextmysite.getText(), edittextmysite.getText().length());
}
}
});
So if anyone enters text it will automatically be appended to the default, like this: https://wwww.mysite.com/<Mytext>
Now what I want is if anyone writes something like this in edittext:
https://wwww.mysite.com/https://wwww.mysite.com/helloworld
or
https://wwww.mysite.com/wwww.mysite.com/helloworld
or
https://wwww.mysite.com/wwww.anyothersite.com/helloworld
that it will automatically convert it to the correct format, like this:
https://wwww.mysite.com/helloworld
How can I achieve this?
#Override
public void afterTextChanged(Editable s) {
if (!s.toString().contains("https://wwww.mysite.com/")) {
String text = s.toString.subString(0, s.lastIndexOf("/"));
edittextmysite.setText(s.toString().replace(text, "https://wwww.mysite.com/");
Selection.setSelection(edittextmysite.getText(), edittextmysite.getText().length());
}
}
edittextmysite.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if(edittextmysite.getText().toString().length() == 0)
edittextmysite.setText("https://wwww.mysite.com/" + s.toString());
else
edittextmysite.append(s.toString());
}
});
Here is what i have tried.
private String str = "https://wwww.mysite.com/";
#Override
public void afterTextChanged(Editable s) {
if (!s.toString().contains("https://wwww.mysite.com/")) {
edittextmysite.setText("https://wwww.mysite.com/");
Selection.setSelection(edittextmysite.getText(), edittextmysite.getText().length());
}
String s1 = s.toString();
String s2 = s1.substring(str.length());
if(s2.contains("/")) {
String s3 = s1.substring(str.length());
if (Patterns.WEB_URL.matcher(s3).matches()) {
// Valid url
edittextmysite.setText(s.toString().replace(s3, ""));
Selection.setSelection(edittextmysite.getText(), edittextmysite.getText().length());
}
}
}
This piece of code won't allow you to enter another URL and user can only enter string after URL as you explained above.
Thanks
Rather that editing the text afterwards, there are many nicer ways to accomplish this:
Place "https://example.com/" on the left of the edittext, then if you really have to, you can search the string for .com, www., etc. and remove it and the name they encapsulate using any algorithm found easily on the web. Then concatenate the strings.
Use a hint in the edittext.
here I have sharing complete working example. There is explanation along with it.
public class MainActivity extends AppCompatActivity implements TextWatcher {
String BASE_URL = "https://wwww.mysite.com";
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*paste this editText --> https://wwww.mysite.com/https://wwww.mysite.com/helloworld <--*/
editText = findViewById(R.id.et);
editText.addTextChangedListener(this);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String text = s.toString().trim();
editText.removeTextChangedListener(this);
if (text.length() > 0) {
if (!text.contains(BASE_URL)) {
String tempText = BASE_URL +"/"+ text;
editText.setText(tempText); //setting text here
proceed(tempText); //sending here for further test, if pasted the link
} else {
proceed(text);
}
}
}
#Override
public void afterTextChanged(Editable s) {
}
private void proceed(String text) {
String newText="";
String firstHalf = text.substring(0,text.lastIndexOf('/'));
String secondHalf = text.substring(text.lastIndexOf('/',(text.length()-1)));
String[] words = firstHalf.split("/"); //Split the word from String
for (int i = 0; i < words.length; i++){ //Outer loop for Comparison
if (words[i] != null) {
for (int j = i + 1; j < words.length; j++){ //Inner loop for Comparison
if (words[i].equals(words[j])) //Checking for both strings are equal
words[j] = null; //Delete the duplicate words
}
}
}
//Displaying the String without duplicate words{
for (int k = 0; k < words.length; k++){
if (words[k] != null)
newText=newText+words[k];
}
StringBuffer formattedText = new StringBuffer((newText+secondHalf));
formattedText.insert(6,"//"); //length of https;//
editText.setText(formattedText);
//attaching textwatcher again
editText.addTextChangedListener(this);
//moving cusor pointer to the end point
editText.setSelection(editText.getText().toString().length());
}
}
You should fix the prefix text into EditText which can not be editable and user only can edit the text after base-url (like after https://wwww.mysite.com/ ).
So you should follow these steps
Prefix the base url to EditText and make it un-editable
Let user enter sub part of the url
Validate input with Patterns.WEB_URL.matcher(inputUrl).matches() for valid url. You can add this validation on TextChange of EditText or on click of a button.
Below is a custom EditText code which you can use directly
public class UrlEditText extends AppCompatEditText {
float mLeftPadding = -1;
public UrlEditText(Context context) {
super(context);
}
public UrlEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public UrlEditText(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
initPrefix();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
String prefix = (String) getTag();
canvas.drawText(prefix, mLeftPadding,
getLineBounds(0, null), getPaint());
}
private void initPrefix() {
if (mLeftPadding == -1) {
String prefix = (String) getTag();
float[] widths = new float[prefix.length()];
getPaint().getTextWidths(prefix, widths);
float textWidth = 0;
for (float w : widths) {
textWidth += w;
}
mLeftPadding = getCompoundPaddingLeft();
setPadding((int) (textWidth + mLeftPadding),
getPaddingRight(), getPaddingTop(),
getPaddingBottom());
}
}
}
and in layout xml file, it would be like
<com.path_of_custom_view.UrlEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="https://wwww.mysite.com/"
android:text="helloworld" />
Instead of using android:tag you can define custom attribute for this edittext.
And for input validation you can validate it like
String enteredUrl = textField.getText().toString();
if (Patterns.WEB_URL.matcher(enteredUrl).matches()) {
// Valid url
} else {
// Invalid url
}
You can Just store it as String and than simply String newReplacedString = stringtoReplace.replace("Phrase To Replace", "WHAT TO REPLACE WITH");
This one works for me, I hope this will work for you too.
#Override
public void afterTextChanged(Editable s) {
String text = edittextmysite.getText().toString();
String URL = "https://www.example.com/";
if (text.contains(URL)) {
String url = getUrl(URL, text);
if (!text.equals(url)) {
edittextmysite.setText(url);
edittextmysite.setSelection(url.length());
}
} else {
String tempUrl = URL + text;
String url = getUrl(URL, tempUrl);
if (!tempUrl.equals(url)) {
edittextmysite.setText(url);
edittextmysite.setSelection(url.length());
} else if (!text.contains(URL)) {
edittextmysite.setText(URL);
edittextmysite.setSelection(URL.length());
}
}
}
private String getUrl(String URL, String text) {
String urls[] = text.split("(?<!/)/(?!/)");
Log.v(TAG, Arrays.toString(urls));
String lastWord = urls[urls.length - 1];
String lastChar = text.substring(text.length() - 1);
if (lastChar.equals("/"))
lastWord = lastWord.concat(lastChar);
for (String url : urls) {
url = url.concat("/");
if (Patterns.WEB_URL.matcher(url).matches()) {
if (url.equals(URL)) {
if (!lastWord.contains("/"))
return url + lastWord;
else return text;
}
}
}
return URL;
}
In this code I tried your inputs, and its working.
It's not an elegant solution and I suggest you to use alternative UX for what you are trying to do entirely but if you really want to pursue this way then try the following code in your TextWatcher,
final String baseString="https://wwww.mysite.com/";
#Override
public void afterTextChanged(Editable s) {
if(!s.toString().contains(baseString)){
editText.setText(baseString+s.toString());
editText.setSelection(editText.length());
}else {
String regex = "\\b(https?|ftp|file)://[-a-zA-Z0-9+&##/%?=~_|!:,.;]*[-a-zA-Z0-9+&##/%=~_|]";
Pattern pattern=Pattern.compile(regex);
String subStr=s.toString().substring(baseString.length());
Matcher matcher= pattern.matcher(subStr);
if(matcher.matches()){
editText.setText(baseString+subStr.replaceAll(regex,""));
editText.setSelection(editText.length());
}else if(subStr.contains("https:")){
editText.setText(baseString+subStr.replace("https:",""));
editText.setSelection(editText.length());
}else if(subStr.contains("www.")){
editText.setText(baseString+subStr.replace("www.",""));
editText.setSelection(editText.length());
}else if(subStr.contains(".")){
editText.setText(baseString+subStr.replaceAll("\\.",""));
editText.setSelection(editText.length());
}else if(subStr.contains("//")){
editText.setText(baseString+subStr.replaceAll("//",""));
editText.setSelection(editText.length());
}else if(subStr.contains(":")){
editText.setText(baseString+subStr.replaceAll(":",""));
editText.setSelection(editText.length());
}
}
}
Once user starts typing, it sets our base string in the edittext and forces
user not to write anything that can be a part of uri. One important thing to consider when user is trying to hit backspace, this is taken care of using the special condition and user won't be able to remove base string once he/she starts typing.
Note: This solution can be optimized as well
Answer
You can set edittext text to not remove by user. So the predefined text will stay with ediitext and automatically append the new text.
Try this:
private EditText et;
private String str_value = "http://example.com/";
private String added_str;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = findViewById(R.id.edittext);
et.setText(str_value);
et.setSelection(str_value.length());
et.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(start == str_value.length() - 1)
{
et.setText(str_value);
et.setSelection(str_value.length());
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
Edited
If you want elimnate the domain name after user entered in the edittext. You can try below code
#Override
public void afterTextChanged(Editable s) {
if(s.length() > str_value.length()) {
added_str = s.toString().substring(str_value.length(), s.length()); //this will get text after predefined text.
if(Patterns.DOMAIN_NAME.matcher(added_str).matches() || added_str.contains("http:"))
{
et.setText(str_value);
et.setSelection(str_value.length());
}
}
}
Related
In recylerview the edittext the values are repeating when fetched through the interface
Recylerview #Override public void onBindViewHolder(#NonNull CustomListview.ViewHolder holder, int position) { holder.Sno.setText(""+(position+1)); //remove colth unwanted characters String cloth = list.get(position).getCloth(); String withoutFirstCharacter = cloth.substring(2,cloth.length()-1); // index starts at zero holder.Cloth.setText(""+withoutFirstCharacter); holder.qty.setText(list.get(position).getQuantity()); holder.QtyId.setText(list.get(position).getQtyId()); holder.Dis.setText(list.get(position).getDispatchedQty()); holder.deliverd is a edittext if (list.get(position).getValue()==null){ holder.deliverd.setText("0"); list.get(position).setValue("0"); } else { holder.deliverd.setText(String.valueOf(list.get(position).getValue())); } holder.deliverd.addTextChangedListener(new TextWatcher() { #Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } #Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (TextUtils.isEmpty(holder.deliverd.getText())) { list.get(position).setValue("0"); } else { list.get(position).setValue(holder.deliverd.getText().toString()); } int a = Integer.parseInt(list.get(position).getValue()); //str is arraylist str.add(Integer.parseInt(list.get(position).getValue())); Log.i( "str",String.valueOf(str)); //interface through pass the values to mainactivity dtInterface.onSetValues(str); // activity.array_val(); } #Override public void afterTextChanged(Editable s) { } }); } Interface public interface DataTransferInterface { public void onSetValues(ArrayList<Integer> in ); } MAinactivity interface implements and override the interface method #Override public void onSetValues(ArrayList<Integer> a ) { HashMap<Integer, ArrayList<Integer>> hm=new HashMap<>(); hm.put(100,a); //display hashmap element Set s = hm.entrySet(); Iterator itr = s.iterator(); while (itr.hasNext()){ Map.Entry m = (Map.Entry)itr.next(); System.out.println(m.getKey()+":values:"+m.getValue()); Toast.makeText(this, "aaa"+m.getValue(),Toast.LENGTH_SHORT).show(); } } when i change the 1st,2nd,3rd edittext are correctly fetched the values one time. The problem is 4th,5th,etc edittext repeating the values 2 times and added to the arraylist. how to slove it.i tried more than 25days.. i attached the error below. [![enter image description here][1]][1] [enter link description here][1] https://andriodretrofit.000webhostapp.com/WhatsAppVideo2020-02-03at83032PM.gif
How to validate a input field and prevent Dialog dismissing
In my application I extend the Dialog class to get user input for each field and now I want to validate them. public abstract class EditDialogHelper extends Dialog implements android.view.View.OnClickListener { private Context context; private String title; private String field; private String positive; private String negative; private EditText etField; private TextView tvCount; private int characterCount; public EditDialogHelper(Context context, String title, String field, String positive, String negative, int characterCount) { super(context); this.context = context; this.title = title; this.field = field; this.positive = positive; this.negative = negative; this.characterCount = characterCount; } #Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.dialog_edit_view); TextView tvTitle = (TextView) findViewById(R.id.tvTitle); etField = (EditText) findViewById(R.id.etField); tvCount = (TextView) findViewById(R.id.tvInputCount); Button btnConfirmationOk = (Button) findViewById(R.id.btnPositive); Button btnConfirmationCancel = (Button) findViewById(R.id.btnNegative); final TextWatcher textWatcher = new TextWatcher() { #Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } #Override public void onTextChanged(CharSequence s, int start, int before, int count) { tvCount.setText(String.valueOf(characterCount - s.length())); } #Override public void afterTextChanged(Editable s) { } }; etField.addTextChangedListener(textWatcher); tvTitle.setText(title); etField.setText(field); etField.setFilters(new InputFilter[]{new InputFilter.LengthFilter(characterCount)}); btnConfirmationOk.setOnClickListener(this); btnConfirmationCancel.setOnClickListener(this); } public String getValue() { return etField.getText().toString().trim(); } private boolean validateInputs(String value) { boolean valid = false; if (value != null && !(value.equals(""))) { valid = true; } else { etField.setError("This can't be left empty"); } return valid; } } Once the dialog opens up I want it to be validated once the btnConfirmationOk is clicked and if the field is empty, it should be prevented from dismissing the dialog while showing the error. Where should I use this validateInputs method and in which way it should be modified.
Answer is quite simple i guess #Override void onClick(View view) { if (view.getId == R.id.btnPositive) { boolean valid = validate("my string"); if(valid) { // do stuff dissmis(); } } else { dissmis(); } } But in my oppinion you should set different listeners to your possitive and negative buttons, instead of tracking everything with EditDialogHelper class. this could be done like this. button.setOnClickListener(new OnClickListener { #Override void onClick(View v) { } }); p.s. I wrote everything from my head so this could contain compilation errors.
Implement Tag functionality like Instagram?
In this demo I want to add tag functionality like Instagram. when I search a follower using # its work properly and show name of that follower. but when I erase # in search it shows error. java.lang.StringIndexOutOfBoundsException: length=0; index=1. I use many code but not solve.need help Code is here:- public class Main2Activity extends AppCompatActivity { RecyclerView following_userr_list, mListView_COmment; EditText editTextSearch; ArrayList<String> FollowingListValuesArr; ArrayList<String> show_following; CustomAdapter adapter; Custom_comment_Adapter adapter1; String final_string = ""; #Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); FollowingListValuesArr = new ArrayList<>(); FollowingListValuesArr.add("Ramiz"); FollowingListValuesArr.add("Karan"); FollowingListValuesArr.add("Azad"); FollowingListValuesArr.add("Manish"); show_following = new ArrayList<>(); show_following.add("Ramiz"); show_following.add("Karan"); show_following.add("Azad"); show_following.add("Manish"); following_userr_list = (RecyclerView) findViewById(recyclerView); editTextSearch = (EditText) findViewById(R.id.editTextSearch); mListView_COmment = (RecyclerView) findViewById(recyclerView_comment); following_userr_list.setHasFixedSize(true); following_userr_list.setLayoutManager(new LinearLayoutManager(this)); mListView_COmment.setHasFixedSize(true); mListView_COmment.setLayoutManager(new LinearLayoutManager(this)); adapter = new CustomAdapter(FollowingListValuesArr); adapter1 = new Custom_comment_Adapter(show_following); following_userr_list.setAdapter(adapter); mListView_COmment.setAdapter(adapter1); editTextSearch.addTextChangedListener(new TextWatcher() { #Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } #Override public void onTextChanged(CharSequence s, int i, int i1, int count) { } #Override public void afterTextChanged(Editable editable) { String s = editable.toString(); final_string = s.substring(1); if (final_string.length() >= 1) { following_userr_list.setVisibility(View.INVISIBLE); mListView_COmment.setVisibility(View.VISIBLE); filters(final_string); } } private void filters(String text) { ArrayList<String> filterdNames = new ArrayList<>(); for (String s : FollowingListValuesArr) { if (s.toLowerCase().contains(text.toLowerCase())) { filterdNames.add(s); } } adapter.filterList(filterdNames); adapter1.filterList(filterdNames); } }); } }
You are getting error in this function. you need to check length of editable before process it. #Override public void afterTextChanged(Editable editable) { String s = editable.toString(); final_string = s.substring(1);// when s is null if (final_string.length() >= 1) { following_userr_list.setVisibility(View.INVISIBLE); mListView_COmment.setVisibility(View.VISIBLE); filters(final_string); } } When string is null you are doing s.substring So add like below #Override public void afterTextChanged(Editable editable) { if (editable.length() > 0) { String s = editable.toString(); final_string = s.substring(1); following_userr_list.setVisibility(View.INVISIBLE); mListView_COmment.setVisibility(View.VISIBLE); filters(final_string); } }
I think You should check the length before going through your logic. #Override public void afterTextChanged(Editable editable) { if(editable.length()>0){ String s = editable.toString(); final_string = s.substring(1); if (final_string.length() >= 1) { following_userr_list.setVisibility(View.INVISIBLE); mListView_COmment.setVisibility(View.VISIBLE); filters(final_string); } } }
TextInputLayout error after enter value into edittext
How can i hide a TextInputLayout error after input one text in EditText. Is it possible? how can i achieve this or I am doing something wrong here.!! code layoutEdtPhone =(TextInputLayout)rootView.findViewById(R.id.layoutEdtPhone); layoutEdtPhone.setErrorEnabled(true); layoutEdtPhone.setError(getString(R.string.ui_no_phone_toast)); layoutEdtPassword = (TextInputLayout)rootView.findViewById(R.id.layoutEdtPassword); layoutEdtPassword.setErrorEnabled(true); layoutEdtPassword.setError(getString(R.string.ui_no_password_toast)); edtPhone=(EditText)rootView.findViewById(R.id.edtPhone); edtPassword=(EditText)rootView.findViewById(R.id.edtPassword); xml <EditText android:id="#+id/edtPhone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:background="#drawable/edt_background_selector" android:drawableLeft="#drawable/phone_icon" android:drawableStart="#drawable/phone_icon" android:hint="#string/phone" android:inputType="phone" android:padding="5dip" android:singleLine="true" android:textSize="14sp" /> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:id="#+id/layoutEdtPassword" android:layout_width="fill_parent" android:layout_height="wrap_content" > <EditText android:id="#+id/edtPassword" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:background="#drawable/edt_background_selector" android:drawableLeft="#drawable/password_icon" android:drawableStart="#drawable/password_icon" android:hint="#string/password" android:inputType="textPassword" android:padding="5dip" android:singleLine="true" android:textSize="14sp" /> </android.support.design.widget.TextInputLayout>
To illustrate further the answer given by Prithviraj, TextInputLayout does not do the validation itself. It is just a mechanism to show the error or hint. You are responsible for setting/clearing the error. Here is how you can do that. Note that in addition to TextChangedListener, you may also need OnFocusChangeListener to set the error when user jumps to second edit text without doing any modification in the first field. protected void onCreate(Bundle savedInstanceState) { //..... edtPhone.addTextChangedListener(new TextWatcher() { #Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } #Override public void onTextChanged(CharSequence s, int start, int before, int count) { } #Override public void afterTextChanged(Editable s) { validateEditText(s); } }); edtPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() { #Override public void onFocusChange(View v, boolean hasFocus) { if (!hasFocus) { validateEditText(((EditText) v).getText()); } } }); } private void validateEditText(Editable s) { if (!TextUtils.isEmpty(s)) { layoutEdtPhone.setError(null); } else{ layoutEdtPhone.setError(getString(R.string.ui_no_password_toast)); } } }
You can set layoutEdtPhone.setErrorEnabled(false);
Do mTextInputLayout.setError(null); to clear the error message. A good practice can be a method to check for errors like this: #Override public void checkErrorBeforeAction() { boolean error = false; mTextInputLayout.setError(null); if (mEditText.getText().toString().length == 0)) { mTextInputLayout.setError("Field empty"); } else if (mEditText.getText().toString().isValid) { // Other condition mTextInputLayout.setError("Field is invalid"); } if (!error) { // Call action } } This way it refreshes the error message before setting a new one.
I used #TextChanged of ButterKnife and worked for me, look: #Bind(R.id.layoutEdtPhone) TextInputLayout tlayoutEdtPhone; #Bind(R.id.edtPhone) EditText edtPhone; //start ButterKnife (I spent the URL with full description for initilize) #OnTextChanged(R.id.edtPhone) public void changedTextOnEditPhone() { tlayoutEdtPhone.setError(""); } If you want know about ButterKnife, I wrote a post with more detail, but it was done in my native language, that is, pt_br. http://blog.alura.com.br/aumentando-a-produtividade-com-butter-knife-no-android/
textInputLatout.getEditText().addTextChangedListener(new TextWatcher() { #Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } #Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (s.length() < 1) { textInputLayout.setErrorEnabled(true); textInputLayout.setError("Please enter a value"); } if (s.length() > 0) { textInputLayout.setError(null); textInputLayout.setErrorEnabled(false); } } #Override public void afterTextChanged(Editable s) { } });
Sadly there is no built-in mechanism to achieve this behavior. I created ViewUtils class that helps me: public final class ViewUtils { private ViewUtils() {} public static void resetTextInputErrorsOnTextChanged(TextInputLayout... textInputLayouts) { for (final TextInputLayout inputLayout : textInputLayouts) { EditText editText = inputLayout.getEditText(); if(editText != null) { editText.addTextChangedListener(new TextWatcher() { #Override public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) { } #Override public void onTextChanged(final CharSequence s, final int start, final int before, final int count) { } #Override public void afterTextChanged(final Editable s) { if(inputLayout.getError() != null) inputLayout.setError(null); } }); } } } } And then you can easily use it in the client code: ViewUtils.resetTextInputErrorsOnTextChanged(mEmailTextInputLayout, mPasswordTextInputLayout);
Using KTX: textInputLayout.editText?.doOnTextChanged { text, start, count, after -> if (text?.any(invalidCharacters.toCharArray()::contains) == true) { textInputLayout.error = "Invalid character entered: ${invalidCharacters}" } else { textInputLayout.error = null } }
This is how I've done it using a custom class that extends TextInputLayout. Uses a TextWatcher like the other answers, but there's no need to manually do anything after using this in the xml. It takes care of everything for you. public class CustomTextInputLayout extends TextInputLayout { public CustomTextInputLayout(Context context) { super(context); } public CustomTextInputLayout(Context context, AttributeSet attrs) { super(context, attrs); } public CustomTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } #Override protected void onFinishInflate() { super.onFinishInflate(); initRemoveErrorWatcher(); } private void initRemoveErrorWatcher() { EditText editText = getEditText(); if (editText != null) { editText.addTextChangedListener(new TextWatcher() { #Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } #Override public void onTextChanged(CharSequence s, int start, int before, int count) { setError(null); } #Override public void afterTextChanged(Editable s) { } }); } } } To use, simply replace TextInputLayout in your xml with this: <com.example.customViews.CustomTextInputLayout>...
If you want to remove the error display from TextInputLayout use:- YourtextInputLayout.setErrorEnabled(false); else If you want to remove error display from edittextfield use:- edittext_id.setError(null); I suggest using Textwatcher fucntion in Android for more efficient validation handling.. eg: editText.addTextChangedListener(new TextWatcher() { #Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } #Override public void onTextChanged(CharSequence s, int start, int before, int count) { } #Override public void afterTextChanged(Editable s) { if (!validatefName()) { return; } } });
Removes error on touch listOf(binding.userName, binding.password).forEach { it.setOnTouchListener { v, _ -> model.error.set(null) v.performClick() false } }
use textwatcher here's the link http://www.learn2crack.com/2014/02/android-textwatcher-example.html
Use a if else condition,if condition is true set TextInoutLayoutObj.setError(null); if its false set your error there
Force close when all characters are deleted from EditText field
My app is force closing when I delete the last of my characters from the edit text box. e.g. if I put in 456, 6 and 5 cause no problems but deleting 4 leads to the error. public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setupViews(); } private void setupViews(){ milesBox = (EditText)findViewById(R.id.enter_miles); try{ milesBox.addTextChangedListener(new TextWatcher(){ #Override public void afterTextChanged(Editable s) { } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } #Override public void onTextChanged(CharSequence s, int start, int before, int count) { addMiles(); } }); }catch (Exception e){ this.showAnswer.setText("TextWatcher error"); this.milesBox.setText(""); } }// end setupviews() public void addMiles(){ try{ String edMiles = this.milesBox.getText().toString(); if(null==edMiles){ this.showAnswer.setText("Please input miles"); this.milesBox.setText(null); } double amiles = Double.parseDouble(edMiles); setMiles(amiles); } catch (Exception e){ this.showAnswer.setText("Please input miles in numbers"); this.milesBox.setText(null); addMiles(); //TODO check if this line causes errors } } public double getMiles() { return miles; } public void setMiles(double miles) { this.miles=miles; }
He broo you have missed else here if(null==edMiles){ this.showAnswer.setText("Please input miles"); this.milesBox.setText(null); } else{ double amiles = Double.parseDouble(edMiles); setMiles(amiles); } here it will give number format exception because u r parsing null string
Change if(null==edMiles){ To if(null == edMiles || edMiles.length() == 0) { or better use TextUtils.isEmpty(edMiles). When you remove last char from edittext the toString method doesn't return null, it returns ""(empty string) as a result.