-5

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.

miken32
  • 42,008
  • 16
  • 111
  • 154
Amit Chavan
  • 13
  • 1
  • 6

3 Answers3

3
 /**
 * 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.

Arbaz Ali Rizvi
  • 253
  • 1
  • 7
1

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);

enter image description here

Ori a
  • 314
  • 1
  • 8
0

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

}

SHISHIR
  • 321
  • 3
  • 4