'how to Replace urls by 4 count in editext if detected
i want to have edittext of 300 char limit but i have a requirement that if detected urls in edittext then it should replace the count by 4 keeping the urls as such
for example :--
If i paste
https://stackoverflow.com/questions/ask-->should display textview count limit 296/300 and also editext to upto 296 char limit for entering
If i paste
https://stackoverflow.com/questions/ask hi-->should display textview count limit 293/300 and also editext to upto 293 char limit for entering
Solution 1:[1]
Sample
Why don't you try this class ? This will calculate the length of the text with a url equal to 4 chars.
import android.content.Context;
import android.util.AttributeSet;
import android.util.Patterns;
import androidx.annotation.NonNull;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
public class CustomEditText extends androidx.appcompat.widget.AppCompatEditText {
private int charCount = 0;
public CustomEdiText(Context context, AttributeSet set) {
super(context, set);
}
public CustomEdiText(@NonNull Context context) {
super(context);
}
@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
super.onTextChanged(text, start, lengthBefore, lengthAfter);
List<String> urls = extractUrls(text.toString());
String removedText = text.toString();
if(urls.size() != 0){
for (String mText: urls) {
removedText = text.toString().replaceAll(mText," ");
}
charCount = removedText.length();
return;
}
charCount = text.toString().length();
}
public int getCharCount(){
return charCount;
}
private List<String> extractUrls(String input) {
List<String> result = new ArrayList<>();
String[] words = input.split("\\s+");
Pattern pattern = Patterns.WEB_URL;
for(String word : words)
if (pattern.matcher(word).find())
result.add(word);
return result;
}
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, type);
List<String> urls = extractUrls(text.toString());
String removedText = text.toString();
if(urls.size() != 0){
for (String mText: urls) {
removedText = text.toString().replaceAll(mText," ");
}
charCount = removedText.length();
return;
}
charCount = text.toString().length();
}
}
You can use it just as a normal edit text class.
To get the length, you can call :
editText.getCharCount();
Don't use .length() or you will get wrong result
This can be a example code:
binding.urlEditText.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) {
Log.d(getClass().getSimpleName(), "afterTextChanged: " + binding.urlEditText.getCharCount());
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
