Is it possible to change part of the color of the hint on android?

I have AppCompatEditTextInside TextInputLayoutand set the tooltip from the resource line.

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

The color of the star does not change to red.

This is what I got.

enter image description here

What I want (edited in Photoshop)

enter image description here

I tried drawableEnd with the image, but AppCompatEditText has a width like "match_parent". Thus, it is displayed at the end. I am not like that. Is there any way to do this.

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. An old hint is not replaced by a new one. Both tips show.

enter image description here

+4
source share
4 answers

To some extent ...

here are my findings

  • Html/Spannablethe Floatingtext TextInputEditTextdoes not work.

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

1

  1. Html/Spannableon TextInputLayouthint does not work.

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

2

  1. Html/Spannableon TextInputEditTextworks.

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

3

, setHint() TextInputEditText (, 3), Floating .

enter image description here

Floating, setHint() TextInputLayout

EditText /

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

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

-

enter image description here

+1
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>
.............................................................
+1

, TextInputLayout.

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

textinputlayout

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

,

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

. api 16 . :

, :

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

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

:

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

, .

0
source

Try using the Spanny library. With this, we can enable and customize each character with different colors, font size, font style and many other things. And it can be used to set prompts, as well as to set text.

This is a link to the Spanny library

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

You can set this Spanny object as follows:

textView.setHint(spanny);

or that:

textView.setText(spanny);

Hope this helps.

0
source

Source: https://habr.com/ru/post/1692633/


All Articles