3

I have a text view. Text view is mandatory so I want * symbol on top of text view in red color.enter image description here

Rahul Sharma
  • 2,741
  • 2
  • 26
  • 38
android_jain
  • 780
  • 8
  • 19

4 Answers4

8
TextView text = (TextView)findViewById(R.id.text);
String simple = "Enter your name ";
String colored = "*";
SpannableStringBuilder builder = new SpannableStringBuilder(simple+colored);

builder.setSpan(new ForegroundColorSpan(Color.RED), simple.lenth(), builder.length(), 
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

text.setText(builder);
Rahul Devanavar
  • 3,627
  • 4
  • 27
  • 58
divyansh ingle
  • 330
  • 1
  • 9
4

One of the easiest way to achieve this by using following code in your strings.xml file and provide color in your color.xml file

<string name="user_name">UserName <font color='red'>*</font></string>
1
    TextView txt_name = (TextView) findViewById(R.id.text);
    String simple = "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);

    txt_name.setText(builder);
Chetan Patel
  • 190
  • 8
0

You can use unicode for "ASTERISK" for that, this is upper version of "*", and use method above with spannable string.