I have a text view. Text view is mandatory so I want *
symbol on top of text view in red color.
Asked
Active
Viewed 4,572 times
3

Rahul Sharma
- 2,867
- 2
- 27
- 40

android_jain
- 788
- 8
- 19
-
use some image inside imageview right of textView whose gravity is top|left – Ankush Bist Jan 18 '17 at 10:29
-
1use * with html.fromhtml(); – Divyesh Patel Jan 18 '17 at 10:30
-
Or use `SpannableString` – N J Jan 18 '17 at 10:31
4 Answers
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,917
- 4
- 32
- 61

divyansh ingle
- 340
- 1
- 9
5
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>

Harshil Rastogi
- 61
- 6
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
- 180
- 8
-
it is working but it is not coming on top i want small icon in top locking like in picture – android_jain Jan 18 '17 at 10:49
-
Hey sourabhjain, why are u used icon for asterisk sign? is there any reason of that ? – Chetan Patel Jan 18 '17 at 10:58
-
no i want just * top end now its coming after text as red color loking like red text i want half size * after text see in image – android_jain Jan 18 '17 at 11:14
-
0
You can use unicode for "ASTERISK" for that, this is upper version of "*", and use method above with spannable string.

Sebastian Staszyński
- 36
- 3