skip to Main Content

I have an AppCompatEditText Inside TextInputLayout and set a hint from string resource.

<string name="company_name">Companyname <font fgcolor='#FF0000'>&#42;</font></string>

The color of the star is not changing to red.

This is what What I got.

enter image description here

What I want (edited in photoshop)

enter image description here

I tried drawableEnd with an image, but the AppCompatEditText has the width as “match_parent”. So its displayed at the end. I don’t what that way. Is there any way to do it.

UPDATE :

Tried this:

company_name.setHint(Html.fromHtml("<small><i>" + "Text Hint Here" + "</i><font color=#FF0000>&#42;</font></small>"));

This is what I got. The old hint is not replaced by new one. Both hints are showing.

enter image description here

5

Answers


  1. Yes it is possible to change the hint color of TextInputLayout. Like this

    <android.support.design.widget.TextInputLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:theme="@style/textinputlayout">
    
     <android.support.v7.widget.AppCompatEditText
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:hint="Company Name"
         android:id="@+id/edit_id"/>
    
    </android.support.design.widget.TextInputLayout>
    

    Create textinputlayout file in styles folder

    <style name="TextLabel" parent="TextAppearance.AppCompat">
      <!-- Hint color and label color in FALSE state -->
      <item name="android:textColorHint">@color/Color Name</item> 
      <item name="android:textSize">20sp</item>
      <!-- Label color in TRUE state and bar color FALSE and TRUE State -->
      <item name="colorAccent">@color/Color Name</item>
      <item name="colorControlNormal">@color/Color Name</item>
      <item name="colorControlActivated">@color/Color Name</item>
    </style>
    

    Set To Main Theme of App,It Works Only Highlight State Only

     <item name="colorAccent">@color/Color Name</item>
    

    Note: This solution is not supported in api 16 or below. For that you are require to do this:

    To change bottom line color, you can use this in your app theme:

    <item name="colorControlNormal">#c5c5c5</item>
    <item name="colorControlActivated">@color/accent</item>
    <item name="colorControlHighlight">@color/accent</item> 
    

    To change floating label color

    <style name="TextAppearence.App.TextInputLayout" parent="@android:style/TextAppearance">
    <item name="android:textColor">@color/main_color</item>
    

    and use it like:

    <android.support.design.widget.TextInputLayout
    ...
     app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"
     android:textColorHint="#c1c2c4">
    

    Hope this helps.

    Login or Signup to reply.
  2. Try using the library named Spanny. With the help of this, we can include and can customize each and every character with different, color, font size, font style and many more things. And it can be used to set the hint as well as setting the text.

    This is the link to the library Spanny

    Spanny spanny = new Spanny("Underline text", new UnderlineSpan())
                .append("Company Name ")
                .append("*", new ForegroundColorSpan(Color.RED));
    

    you can set that Spanny object by this:

    textView.setHint(spanny);
    

    or this:

    textView.setText(spanny);
    

    Hope it helps.

    Login or Signup to reply.
  3. change edittext hint color :-
     android:backgroundTint="@color/colorAccent"
    TextInputLayout
     android:textColorHint="#707E90"
    ....................................................
        <android.support.design.widget.TextInputLayout
            android:id="@+id/txt_usemail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColorHint="#707E90">
    
            <EditText
                android:id="@+id/et_usemaill"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:backgroundTint="@color/colorAccent"
                android:hint="@string/res_email"
                android:inputType="textEmailAddress"
                android:textColor="@color/textcolor" />
    
        </android.support.design.widget.TextInputLayout>
    .............................................................
    
    Login or Signup to reply.
  4. To some extent possible …

    here are my findings

    1. Html/Spannable on Floating text of TextInputEditText is not working.

      txt_usemail.setHint(Html.fromHtml("<font color="#707E90">" + "Companyname" + "</font>" + "<font color="#FF0000">" + "*" + "</font>"));

    1

    1. Html/Spannable on TextInputLayout hint is not working.

      txt_usemail.setHint(Html.fromHtml("<font color="#707E90">" + "Companyname" + "</font>" + "<font color="#FF0000">" + "*" + "</font>"));

    2

    1. Html/Spannable on TextInputEditText is working.

      et_usemaill.setHint(Html.fromHtml("<font color="#707E90">" + "Companyname" + "</font>" + "<font color="#FF0000">" + "*" + "</font>"));

    3

    Now, problem here is if you setHint() on TextInputEditText(like point 3) then Floating text won’t work.

    enter image description here

    to use Floating text, we need to setHint() on TextInputLayout

    Here is the trick using EditText focus/defocus

    final TextInputLayout txt_usemail = findViewById(R.id.txt_usemail);
    final TextInputEditText et_usemaill = findViewById(R.id.et_usemaill);
    
    
    et_usemaill.setHint(Html.fromHtml("<font color="#707E90">" + "Companyname" + "</font>" + "<font color="#FF0000">" + "*" + "</font>"));
    
    et_usemaill.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
    
                if (et_usemaill.getText().toString().equals("")) {
    
                    if (hasFocus) {
                        et_usemaill.setHint(null);
                        txt_usemail.setHint(Html.fromHtml("<font color="#c5c5c5">" + "Companyname" + "</font>" + "<font color="#FF0000">" + "*" + "</font>"));
                    } else {
                        // Remove Glitch/Smooth Animation
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                txt_usemail.setHint(null);
                                et_usemaill.setHint(Html.fromHtml("<font color="#707E90">" + "Companyname" + "</font>" + "<font color="#FF0000">" + "*" + "</font>"));
                            }
                        }, 200);
                    }
                }
    
            }
        });
    

    style

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorControlNormal">#c5c5c5</item>
        <item name="colorControlActivated">#c5c5c5</item>
        <item name="colorControlHighlight">#c5c5c5</item>
    </style>
    
    <style name="TextAppearence.App.TextInputLayout" parent="@android:style/TextAppearance">
        <item name="android:textColor">@android:color/holo_red_dark</item>
        <item name="android:textSize">12sp</item>
    </style>
    

    layout

    <android.support.design.widget.TextInputLayout
        android:layout_marginTop="56dp"
        android:id="@+id/txt_usemail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"
        android:textColorHint="@android:color/holo_red_dark"
        >
    
        <android.support.design.widget.TextInputEditText
            android:id="@+id/et_usemaill"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:inputType="textEmailAddress"
            android:textColor="@color/white" />
    
    </android.support.design.widget.TextInputLayout>
    

    Result –

    enter image description here

    Login or Signup to reply.
  5. Just set hint for the
    <com.google.android.material.textfield.TextInputLayout>
    that contains
    —— <EditText>
    in your code

    Code (Kotlin):

    your_til_id.hint = Html.fromHtml(getString(R.string.your_hint_str)+ "<font color='red'>*</font>")
    

    Layout:

    <com.google.android.material.textfield.TextInputLayout 
        android:id="@+id/your_til_id"
        android:layout_width="0dp"
        android:layout_height="wrap_content">
        <EditText 
            android:id="@+id/your_et_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </com.google.android.material.textfield.TextInputLayout>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search