Is there a way to set multiple colors to the EditText
hint when wrapped by android.support.design.widget.TextInputLayout
without compromising the behavior of floating EditText Hint
?
** e.g:** Headline* 'Headline' in black color and '*' in red
Literally, my question is an exact duplicate of this question: Multicolored edittext hint .
I am posting it again because there is no valid answer to that question. (Please don't mark my question as a duplicate to that one !!)
I have tried the answer given there an this was the outcome :
You can see that the floating property of the TextInputLayout
is no longer applicable. (Want hint to float like this)
Is there anyway to maintain the behaviour of the TextInputLayout EditText
with multi-color hint?
Code:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingEnd="16dp"
android:paddingStart="16dp"
android:paddingTop="16dp">
<EditText
android:id="@+id/appointment_patient_name_et_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:inputType="textPersonName"
android:textColor="@color/colorAccent" />
</android.support.design.widget.TextInputLayout>
Attempt to set multi-color hint :
appointment_patient_name_et = findViewById(R.id.appointment_patient_name_et_id);
Spannable wordtoSpan = new SpannableString("Hello world");
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 2, 6,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
appointment_patient_name_et.setHint(wordtoSpan);
Edit 1: After using git library suggested by ADM...
.java :
MaterialEditText appointment_patient_name_et;
appointment_patient_name_et = findViewById(R.id.appointment_patient_name_et_id);
// Multicolor hint
final Spannable spannable1 = new SpannableString("Patient's Name *");
spannable1.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorPrimary)), 0,
spannable1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable1.setSpan(new ForegroundColorSpan(Color.RED),
spannable1.length() - 1, spannable1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Spannable spannable2 = new SpannableString("Patient's Name *");
spannable2.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorAccent)), 0,
spannable2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable2.setSpan(new ForegroundColorSpan(Color.RED),
spannable2.length() - 1, spannable2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
appointment_patient_name_et.setHint(spannable1);
appointment_patient_name_et.setFloatingLabelText(spannable2);
appointment_patient_name_et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
appointment_patient_name_et.setFloatingLabelAlwaysShown(true);
appointment_patient_name_et.setHint("");
} else {
appointment_patient_name_et.setFloatingLabelAlwaysShown(false);
appointment_patient_name_et.setHint(spannable1);
}
}
});
.xml:
<com.rengwuxian.materialedittext.MaterialEditText
android:id="@+id/appointment_patient_name_et_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
android:hint="Patient's Name *"
app:met_baseColor="@color/colorAccent"
app:met_clearButton="true"
app:met_floatingLabel="highlight"
app:met_floatingLabelTextColor="@color/colorAccent"
app:met_primaryColor="@color/colorAccent"
app:met_singleLineEllipsis="true"
app:met_textColor="@color/colorAccent"
app:met_textColorHint="@color/colorPrimary" />
Output :
Without gaining focus :
Is there any way to make the asterisk in the floating hint in a different color than the floating hint color?