How can I highlight in red the text that's beyond the limit of the EditText like so:
For example, I want a 100 character limit. How can I do it? This is from YouTube app.
How can I highlight in red the text that's beyond the limit of the EditText like so:
For example, I want a 100 character limit. How can I do it? This is from YouTube app.
You can use spannable to paint different part of the text of an EditText:
final SpannableStringBuilder sb = new SpannableStringBuilder(charSequence.toString());
final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(158, 0, 0));
//final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); // You can make the text bold!
sb.setSpan(fcs, yourTextLimit, charSequence.toString().length() - 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
reference :This answer
Set an onChanged listener to your EditText and when its text size exceeds the limit, you can use the code above to change the color of text
public class MainActivity extends AppCompatActivity {
EditText test;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test = (EditText) findViewById(R.id.test);
test.addTextChangedListener(new TextWatcher() {
boolean changed = true;
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
int yourTextLimit = 10;
final SpannableStringBuilder sb = new SpannableStringBuilder(charSequence.toString());
if (charSequence.toString().length() > yourTextLimit && changed) {
final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(158, 0, 0));
//final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); // You can make the text bold!
// sb.setSpan(bss, yourTextLimit, charSequence.toString().length() - 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
sb.setSpan(fcs, yourTextLimit, charSequence.toString().length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
sb.setSpan(new MyClickableSpan(charSequence.toString()), 0, charSequence.toString().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
changed = false;
test.setText(sb);
} else if (changed) {
final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(0, 0, 0));
sb.setSpan(fcs, 0, charSequence.toString().length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
sb.setSpan(new MyClickableSpan(charSequence.toString()), 0, charSequence.toString().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
changed = false;
test.setText(sb);
}
test.setSelection(charSequence.toString().length());
}
@Override
public void afterTextChanged(Editable editable) {
changed = true;
}
});
}
class MyClickableSpan extends ClickableSpan {// extend ClickableSpan
String clicked;
public MyClickableSpan(String string) {
super();
clicked = string;
}
public void onClick(View tv) {
Toast.makeText(MainActivity.this,clicked , Toast.LENGTH_SHORT).show();
}
public void updateDrawState(TextPaint ds) {// override updateDrawState
ds.setUnderlineText(false); // set to false to remove underline
}
}
}