How do you get a red asterisk in an entry so that you can display it at the end of the text (or for that matter anywhere within the text) to indicate it's a required field?
Example:
Enter your name *
The above asterisk would be red.
How do you get a red asterisk in an entry so that you can display it at the end of the text (or for that matter anywhere within the text) to indicate it's a required field?
Example:
Enter your name *
The above asterisk would be red.
/**
* For setting mandatory fields symbol * * @param hintData * @return
*/
public SpannableStringBuilder setMandatoryHintData(String hintData) {
String simple = hintData;
String colored = " *";
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(simple);
int start = builder.length();
builder.append(colored);
int end = builder.length();
builder.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorGrayLight_75)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return builder;
}
Use can use Above code with Edittext and set your hint grammatically. There is no way to set red astrik on InputTextLayout.
For this you need to use SpannableStringBuilder and ForegroundColorSpan.
another way of doing so:
TextView text = (TextView)findViewById(R.id.text);
String simple = "Enter your name ";
String colored = "*";
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(simple);
int start = builder.length();
builder.append(colored);
int end = builder.length();
builder.setSpan(new ForegroundColorSpan(Color.RED), start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setText(builder);
Use this extension for AppCompatEditText.
fun AppCompatEditText.addAsteriskSign() {
val colored = "*"
val builder = SpannableStringBuilder()
builder.append(this.hint)
val start = builder.length
builder.append(colored)
val end = builder.length
builder.setSpan(
ForegroundColorSpan(Color.RED), start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
this.hint = builder
}