I want to show mandatory fields using TextView which includes red colored asterisk
My Output
Required Output
I tried modifying the code, but didn't get the expected result.
My Code:
public void setCompulsoryAsterisk() {
txt_name="Name : ";
String colored="*";
SpannableStringBuilder strBuilder=new SpannableStringBuilder();
strBuilder.append(txt_name);
int start=strBuilder.length();
strBuilder.append(colored);
int end=strBuilder.length();
strBuilder.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txtName.setText(strBuilder);
}
Try Something Like This:
string = "<font color='#000000'>Name </font>" + "<font color='#FF0000'>*</font>" + "<font color='#000000'>:</font>";
textView.setText(Html.fromHtml(string));
You can use an unicode to show asterisk in textView like
Name* :
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\u002A"
android:textColor="color/red"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" :"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With