1

I have a mandatory EditText that has a placeholder with mandatory symbol * that has to be red. Help with this guys.

Like this

Kaushik
  • 6,150
  • 5
  • 39
  • 54

2 Answers2

6

Try like this, try this discussion for more detail.

TextView text = (TextView)findViewById(R.id.text);

String simple = "First 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.setHint(builder);
Community
  • 1
  • 1
Remees M Syde
  • 2,564
  • 1
  • 19
  • 42
-1

Create your own custom view with its structure like

<RelativeLayout 
    android:layout_width="100dp" 
    android:layout_height="40dp">
    <EditText
        android:layout_width="100dp" 
        android:layout_height="40dp"/>
    <TextView
        android:layout_width="wrap_content" 
        android:layout_height="40dp" 
        android:id="@+id/placeholder_left"
        android:text="First name"/>
    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="40dp" 
        android:layout_toRightOf="@+id/placeholder_left" 
        android:textColor="@color/red"
        android:text="*"/>

Add logic to handle the visibility of the "placeholder" TextView upon text change in the EditText.

Patrick Chan
  • 1,019
  • 10
  • 14