I am using TextInputLayout in which i want to change the color of hinttext when i enter text then the color of hint text should be blue other wise color should be gray and when edittext contains some value then the color of hintText should blue for more information please check my image.
Ultimate i want this after edit text containing some value.
This is my current result after using this code
<style name="labelcolor" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Hint color and label color in FALSE state -->
<item name="android:textColorHint">#868686</item>
<item name="android:textSize">16sp</item>
<item name="android:paddingTop">5sp</item>
<!-- Label color in TRUE state and bar color FALSE and TRUE State -->
<item name="colorAccent">#0ea3ff</item>
<item name="colorControlNormal">#0ea3ffr</item>
<item name="colorControlActivated">#0ea3ff</item>
</style>
please help me how can i solve this issues.
Change TextInputLayout Lebel Color Dynamicaly in on Focus change of edittext and pass color according work for me.
public static void textInoutLayoutColor(TextInputLayout textInputLayout, #ColorInt int color) {
try {
Field fDefaultTextColor = TextInputLayout.class.getDeclaredField("mDefaultTextColor");
fDefaultTextColor.setAccessible(true);
fDefaultTextColor.set(textInputLayout, new ColorStateList(new int[][]{{0}}, new int[]{ color }));
Field fFocusedTextColor = TextInputLayout.class.getDeclaredField("mFocusedTextColor");
fFocusedTextColor.setAccessible(true);
fFocusedTextColor.set(textInputLayout, new ColorStateList(new int[][]{{0}}, new int[]{ color }));
} catch (Exception e) {
e.printStackTrace();
}
}
and change the color like this.
input_layout= (TextInputLayout) findViewById(R.id.input_layout);
etuser_name.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasfocus) {
if(hasfocus){
textInoutLayoutColor(input_layout,getResources().getColor(R.color.bullet_color));
}
else
{
if(etuser_name.getText().toString().trim().length()>0) {
textInoutLayoutColor(input_layout,getResources().getColor(R.color.bullet_color));;
}
else
{
textInoutLayoutColor(input_layout,getResources().getColor(R.color.hint_grey));
}
}
}
});
Create your as bellow and try
<style name="EditTextHint" parent="TextAppearance.AppCompat">
<item name="colorAccent">#android:color/white</item>
<item name="android:textColorHint">#color/BackgroundtWhiteColor</item>
<item name="colorControlNormal">#color/BackgroundtWhiteColor</item>
<item name="colorControlActivated">#color/your color</item>
<item name="colorControlHighlight">#color/BackgroundtWhiteColor</item>
</style>
Use it as bellow
<android.support.design.widget.TextInputLayout
android:theme="#style/EditTextHint"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>
Firstly you need to set your TextInputLayout
android.support.design.widget.TextInputLayout textInputLayout = (TextInputLayout) view.findViewById(R.id.textInputLayout);
textInputLayout.setHintTextAppearance(R.style.Instyle);
and then in styles.xml file you can create your styles with the color for the hint color.
<style name="Instyle" parent="AppThemeLight">
<itemname="android:textColorPrimary">#android:color/darker_gray</item>
<item name="android:textColor">#android:color/darker_gray</item>
</style>
TextInputLayout takes colorAccent to hintTextColor when you start entering values in edittext.
So if you want it to be blue then set your colorAccent to blue colour in your theme. And when edittext has no value then set hintTextColor to grey. Do like this
<android.support.design.widget.TextInputLayout
android:id="#+id/userNameTIL"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="#color/spinnerBg"
android:clipToPadding="false"
android:gravity="bottom"
android:paddingTop="4dp"
android:textColorHint="#color/grey"> /*Here it sets color to hintText when edittext has no value */
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:background="#null"
android:hint="Username"
android:inputType="text"
android:maxLength="30"
android:paddingEnd="3dp"
android:paddingStart="25dp"
android:paddingTop="3dp"
android:singleLine="true"
android:textColor="#color/black"
android:textColorHint="#color/black"
android:textSize="20sp"/>
</android.support.design.widget.TextInputLayout>
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!--EditText hint color-->
<item name="android:textColorHint">#color/default_app_white</item> <!-- TextInputLayout text color-->
<item name="colorControlActivated">#color/default_app_green</item>
<!-- EditText line color when EditText on-focus-->
<item name="colorControlHighlight">#color/default_app_green</item>
<!-- EditText line color when EditText in un-focus-->
<item name="colorControlNormal">#color/default_app_white</item>
</style>
<!-- In your style.xml file -->
try this one but it affects your whole application
Related
I'm trying to change the color of AlertDialog with MultiChoiceItems
Java :
private void displayMultiSelectDialog() {
emoji = getResources().getStringArray(R.array.photo_editor_emoji);
boolean[] checkedItems = new boolean[emoji.length];
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this, R.style.DialogTheme);
dialogBuilder.setTitle("Select Emoji");
dialogBuilder.setMultiChoiceItems(convertListEmoji(emoji), checkedItems,
(dialogInterface, which, isSelected) -> {
if (isSelected) {
selectedEmoji.add(emoji[which]);
} else {
selectedEmoji.remove(emoji[which]);
}
}
);
dialogBuilder.setPositiveButton("Done", (dialog, which) -> showSelectedColors());
dialogBuilder.create().show();
}
XML :
<style name="DialogTheme">
<item name="android:background">#000</item>
<item name="android:textColor">#586eea</item>
<item name="android:textSize">18sp</item>
<item name="android:textColorPrimary">#586eea</item>
<item name="android:colorAccent" tools:targetApi="lollipop">#586eea</item>
</style>
but I have a problem my AlertDialog background, it's BLACK so the checkbox items look invisible
how to make the checkbox looks like this :
Massive thanks in advance
Try setting a custom theme to your checkbox.
Add style in your styles.xml
<style name="MyCheckBox" parent="Theme.AppCompat.NoActionBar">
<item name="colorControlNormal">#000</item> <!-- normal border color -->
<item name="colorControlActivated">#000</item> <!-- activated color -->
<item name="android:textColor">#FFFF3F3C</item> <!-- text color -->
</style>
and set this style as your checkbox's theme as below:
<CheckBox android:id="#+id/check_agree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="I agree"
android:theme="#style/MyCheckBox"/> <!-- here apply your checkbox style -->
<CheckBox
...
android:buttonTint="#color/tint_color" />
minSdkVersion is 21+ use android:buttonTint attribute to update the color of a checkbox:
I am having an issue with the TextInputLayout error state. When I set the error on the TextInputLayout, then error message is red, along with the underline, but the hint does not (it stays the default colour - grey). I have played around with setting the errorTextAppearance, yet I cannot seem to get the hint text to change colour.
<android.support.design.widget.TextInputLayout
android:id="#+id/textInputLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="My Hint"
app:errorTextAppearance="#style/MyErrorTextAppearance">
<android.support.design.widget.TextInputEditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:maxLength="10"
android:maxLines="1" />
styles.xml
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="MyErrorTextAppearance" parent="TextAppearance.Design.Error">
<item name="android:textColor">#color/colorError</item>
</style>
I set and clear the error as follows:
myTextInputLayout.setErrorEnabled(true);
myTextInputLayout.setError(errorMessage);
myTextInputLayout.setError(null);
myTextInputLayout.setErrorEnabled(false);
try following code:
fun setHintColor(#ColorInt color: Int)
{
try {
var stateList = ColorStateList(arrayOf(intArrayOf(0)), intArrayOf(color))
val defaultTextColorField = TextInputLayout::class.java.getDeclaredField("mDefaultTextColor")
defaultTextColorField?.let {field->
field.isAccessible = true
field.set(this, stateList)
}
val focusedTextColorField = TextInputLayout::class.java.getDeclaredField("mFocusedTextColor")
focusedTextColorField?.let{field->
field.isAccessible = true
field.set(this, stateList)
}
}catch(e: java.lang.Exception){
// handle exception
}
}
I have an AppCompatEditText Inside TextInputLayout and set a hint from string resource.
<string name="company_name">Companyname <font fgcolor='#FF0000'>*</font></string>
The color of the star is not changing to red.
This is what What I got.
What I want (edited in photoshop)
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>*</font></small>"));
This is what I got. The old hint is not replaced by new one. Both hints are showing.
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>
.............................................................
To some extent possible ...
here are my findings
Html/Spannable on Floating text of TextInputEditText is not working.
txt_usemail.setHint(Html.fromHtml("<font color=\"#707E90\">" + "Companyname" + "</font>" + "<font color=\"#FF0000\">" + "*" + "</font>"));
Html/Spannable on TextInputLayout hint is not working.
txt_usemail.setHint(Html.fromHtml("<font color=\"#707E90\">" + "Companyname" + "</font>" + "<font color=\"#FF0000\">" + "*" + "</font>"));
Html/Spannable on TextInputEditText is working.
et_usemaill.setHint(Html.fromHtml("<font color=\"#707E90\">" + "Companyname" + "</font>" + "<font color=\"#FF0000\">" + "*" + "</font>"));
Now, problem here is if you setHint() on TextInputEditText(like point 3) then Floating text won't work.
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 -
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.
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>
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.
I'm using TextInputLayout with floating label hints. But In the normal state I am unable to change the hint color from white to other color. Is there a way to do this ?
<android.support.design.widget.TextInputLayout
android:id="#+id/fullNameTextLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="0.75">
<EditText
android:id="#+id/etFullName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp"
android:layout_marginEnd="15dp"
android:layout_marginRight="15dp"
android:singleLine="true"
android:hint="Full Name"
android:textColor="#color/gray_dark"
android:textColorHint="#color/green"
android:textColorHighlight="#color/green" />
</android.support.design.widget.TextInputLayout>
Attaching two screen shots with the background changed.
Please add this in TextInputLayout,
app:hintTextAppearance="#style/mytext
So your layout will be like :
<android.support.design.widget.TextInputLayout
android:id="#+id/aeal_input_layout_zipcode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/green"
app:hintTextAppearance="#style/mytext">
<EditText
android:id="#+id/aeal_etZipCode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Zipcode"
android:singleLine="true"
android:inputType="number"
android:textColor="#color/primaryTextColor" />
</android.support.design.widget.TextInputLayout>
style.xml:
<style name="mytext" parent="#android:style/TextAppearance">
<item name="android:textColor">#color/green</item>
<item name="android:textColorHint">#color/green</item>
<item name="colorAccent">#color/green</item>
<item name="android:textSize">14sp</item>
</style>
Edited : You need to add textColorHint in TextInputLayout, and it will work properly as you needed.
It worked for me, so might help you too.
Got the same probleme and solved this with the setHintTextAppearance(int) method of TextInputLayout.
Example:
First find your TextInputLayout
android.support.design.widget.TextInputLayout textInputLayout = (TextInputLayout) view.findViewById(R.id.textInputLayout);
now do the following:
textInputLayout.setHintTextAppearance(R.style.Inactive);
in your styles.xml file you create your styles with the color for the hint color. For example
<style name="Inactive" parent="AppThemeLight">
<item name="android:textColorPrimary">#android:color/darker_gray</item>
<item name="android:textColor">#android:color/darker_gray</item>
</style>
<style name="Active" parent="AppThemeLight">
<item name="android:textColorPrimary">#android:color/black</item>
<item name="android:textColor">#android:color/black</item>
</style>
now, when you change things for example onButtonClick, you simply change your style.
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textInputLayout.setHintTextAppearance(R.style.Active);
}
});
Keep in mind, that you also have to handle the Colors for your EditText inside your TextInputLayout.
Use this to change the hint color. -
editText.setHintTextColor(getResources().getColor(R.color.xyz));
Solution for your problem -
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,int arg3){
//do something
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
//do something
}
#Override
public void afterTextChanged(Editable arg0) {
if(arg0.toString().length() <= 0) //check if length is equal to zero
editText.setHintTextColor(getResources().getColor(R.color.xyz));
}
});
In your Style.xml you can change the color of hint
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorAccent">#3498db</item>
</style>
For more see this
try this
<android.support.design.widget.TextInputLayout
........
android:textColorHint="requiredColor"
....> to change the floating hint color and
<EditText
android:textColor="requiredColor" to change the hint color in edittxt
Someone tried to change the font of the floating label? I changed the source of EditText but the font of the floating label did not change, I am very grateful to those who help me
Code:
<android.support.design.widget.TextInputLayout
android:id="#+id/tilTextoDescricao"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/tilValorUnidade"
android:layout_marginTop="10dp">
<EditText
android:id="#+id/etTextoDescricao"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:hint="Descrição"
android:textSize="15dp"
android:inputType="text" />
</android.support.design.widget.TextInputLayout>
-----------------
etTextoDescricao= (EditText) findViewById(R.id.etTextoDescricao);
etTextoDescricao.setTypeface(CustomTypeface.getTypefaceMediumDefault(this));
As of Design Library v23, you can use TextInputLayout#setTypeface().
This will set the typeface on both the expanded and floating hint.
Here is the feature request where it was discussed on b.android.com.
EDIT: The error view typeface was not being set, but is now fixed in v25.1.0.
Unfortunately, you'll have to use reflection to handle this.
The floating label is drawn by CollapsingTextHelper, which is an internal, package-private class and isn't setup to handle spans. So, using something like a custom TypefaceSpan won't work in this case.
Because this uses reflection, it isn't guaranteed to work in the future.
Implementation
final Typeface tf = Typeface.createFromAsset(getAssets(), "your_custom_font.ttf");
final TextInputLayout til = (TextInputLayout) findViewById(R.id.yourTextInputLayout);
til.getEditText().setTypeface(tf);
try {
// Retrieve the CollapsingTextHelper Field
final Field cthf = til.getClass().getDeclaredField("mCollapsingTextHelper");
cthf.setAccessible(true);
// Retrieve an instance of CollapsingTextHelper and its TextPaint
final Object cth = cthf.get(til);
final Field tpf = cth.getClass().getDeclaredField("mTextPaint");
tpf.setAccessible(true);
// Apply your Typeface to the CollapsingTextHelper TextPaint
((TextPaint) tpf.get(cth)).setTypeface(tf);
} catch (Exception ignored) {
// Nothing to do
}
Error view
If you needed to change the font of the error, you could do one of two things:
Use Reflection grab the error TextView and apply the Typeface much like before
Use a custom span. Unlike the floating label, the error view used by TextInputLayout is just a TextView, so it's able to handle spans.
Using reflection
final Field errorField = til.getClass().getDeclaredField("mErrorView");
errorField.setAccessible(true);
((TextView) errorField.get(til)).setTypeface(tf);
Using a custom span
final SpannableString ss = new SpannableString("Error");
ss.setSpan(new FontSpan(tf), 0, ss.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
til.setError(ss);
private static final class FontSpan extends MetricAffectingSpan {
private final Typeface mNewFont;
private FontSpan(Typeface newFont) {
mNewFont = newFont;
}
#Override
public void updateDrawState(TextPaint ds) {
ds.setTypeface(mNewFont);
}
#Override
public void updateMeasureState(TextPaint paint) {
paint.setTypeface(mNewFont);
}
}
Results
The font I'm using is Smoothie Shoppe.
I'm using new MaterialComponents theme and none of the answers helped me.
Had to play with styles and themes on my own. Will post a chunk of styles here in case somebody faces the same issue.
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
...
<item name="textInputStyle">#style/CustomFontTextInputLayout</item>
</style>
<!-- region TextInputLayout & TextInputEditText styles -->
<style name="TextInputLayout.OutlineBox.CustomFont" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="android:theme">#style/ThemeOverlay.TextInputEditText.OutlinedBox.CustomFont</item>
</style>
<style name="ThemeOverlay.TextInputEditText.OutlinedBox.CustomFont" parent="ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox">
<item name="editTextStyle">#style/TextInputEditText.OutlinedBox.CustomFont</item>
</style>
<style name="TextInputEditText.OutlinedBox.CustomFont" parent="Widget.MaterialComponents.TextInputEditText.OutlinedBox">
<item name="android:fontFamily">#font/my_font</item>
</style>
<style name="CustomFontTextInputLayout" parent="Widget.Design.TextInputLayout">
<item name="hintTextAppearance">#style/TextInputLayoutHintText</item>
<item name="helperTextTextAppearance">#style/TextInputLayoutHelperText</item>
<item name="errorTextAppearance">#style/TextInputLayoutErrorText</item>
</style>
<style name="TextInputLayoutHintText" parent="TextAppearance.Design.Hint">
<item name="android:fontFamily">#font/my_font</item>
</style>
<style name="TextInputLayoutHelperText" parent="TextAppearance.Design.HelperText">
<item name="android:fontFamily">#font/my_font</item>
</style>
<style name="TextInputLayoutErrorText" parent="TextAppearance.Design.Error">
<item name="android:fontFamily">#font/my_font</item>
</style>
<!-- endregion -->
Then in xml layout:
<android.support.design.widget.TextInputLayout
style="#style/TextInputLayout.OutlineBox.CustomFont"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/first_name"/>
</android.support.design.widget.TextInputLayout>
Here's the result:
i just found a simple solution and it's worked for me:
in this way you can set the typeface to hint of any edit text:
in layout.xml :
<android.support.design.widget.TextInputLayout
android:id="#+id/text_input1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/edt_user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/username"/>
</android.support.design.widget.TextInputLayout>
and in java class :
public class MainActivity extends AppCompatActivity {
EditText editText;
TextInputLayout textInputLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Typeface font_yekan= Typeface.createFromAsset(getAssets(), "fonts/byekan.ttf");
textInputLayout= (TextInputLayout) findViewById(R.id.text_input1);
textInputLayout.setTypeface(font_yekan);
}
}
Here is a custom class implementation for adneal's answer.
public class CustomTextInputLayout extends TextInputLayout {
public CustomTextInputLayout(Context context) {
super(context);
initFont(context);
}
public CustomTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initFont(context);
}
private void initFont(Context context) {
final Typeface typeface = Typeface.createFromAsset(
context.getAssets(), "fonts/YOUR_CUSTOM_FONT.ttf");
EditText editText = getEditText();
if (editText != null) {
editText.setTypeface(typeface);
}
try {
// Retrieve the CollapsingTextHelper Field
final Field cthf = TextInputLayout.class.getDeclaredField("mCollapsingTextHelper");
cthf.setAccessible(true);
// Retrieve an instance of CollapsingTextHelper and its TextPaint
final Object cth = cthf.get(this);
final Field tpf = cth.getClass().getDeclaredField("mTextPaint");
tpf.setAccessible(true);
// Apply your Typeface to the CollapsingTextHelper TextPaint
((TextPaint) tpf.get(cth)).setTypeface(typeface);
} catch (Exception ignored) {
// Nothing to do
}
}
}
In your XML files now you need to use CustomTextInputLayout instead of TextInputLayout and it will work out of the box.
<your.package.CustomTextInputLayout
android:id="#+id/textInputLayout_email"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<AutoCompleteTextView
android:id="#+id/editText_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_email"
android:inputType="textEmailAddress" />
Thanks adneal for the answer.
final Typeface tf = Typeface.createFromAsset(getAssets(), "your_custom_font.ttf");
final TextInputLayout til = (TextInputLayout) findViewById(R.id.yourTextInputLayout);
til.getEditText().setTypeface(tf);
til.setTypeface(tf);
There is a simpler way,
Create a new directory in your 'res' folder named 'font' and put a font in there. Then open your 'styles' file and create a new style :
<style name="customfontstyle" parent="#android:style/TextAppearance.Small">
<item name="android:fontFamily">#font/poppins_regular</item>
</style>
You can add more properties as well, such as textColor, textSize etc..
Then in your XML:
<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:hintTextAppearance="#style/customfontstyle"
>
<android.support.design.widget.TextInputEditText
android:layout_width="220dp"
android:layout_height="wrap_content"
android:id="#+id/edit_phone_number"
android:hint="#string/phone_number_label"
android:inputType="number"
/>
</android.support.design.widget.TextInputLayout>
I checked it and it works.
Use can use style.xml like below:
Style file:
<style name="TextInputLayoutErrorStyle" parent="TextAppearance.Design.Error">
<item name="fontFamily">#font/iran_sans_medium</item>
<item name="android:fontFamily">#font/iran_sans_medium</item>
</style>
<style name="TextInputLayoutHintStyle" parent="TextAppearance.Design.Hint">
<item name="fontFamily">#font/iran_sans_medium</item>
<item name="android:fontFamily">#font/iran_sans_medium</item>
</style>
<style name="TextInputLayoutHelperStyle" parent="TextAppearance.Design.HelperText">
<item name="fontFamily">#font/iran_sans_medium</item>
<item name="android:fontFamily">#font/iran_sans_medium</item>
</style>
<style name="TextInputLayoutOutlinedBoxStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="helperTextTextAppearance">#style/TextInputLayoutHelperStyle</item>
<item name="errorTextAppearance">#style/TextInputLayoutErrorStyle</item>
<item name="hintTextAppearance">#style/TextInputLayoutHintStyle</item>
</style>
Layout file:
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:hint="#string/cardname_hint"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
card_view:helperText="#string/cardname_helper"
style="#style/TextInputLayoutOutlinedBoxStyle"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:fontFamily="#font/iran_sans_medium"
android:textColor="#color/colorTextPrimary"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
I was looking for this, I found this way, using the support library:
Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);
and set this typeface to yout TextInpuLayout.
For me works like charm, I hope it helps others =]
Source: Documentation
fixing a problem in #adneal answer:
if setErrorEnabled is not set true, mErrorView would be null and if you set it false at any point the font would change back to default.
so to fix it:
in you custom TextInputLayout override setErrorEnabled
#Override
public void setErrorEnabled(boolean enabled) {
super.setErrorEnabled(enabled);
if (enabled) {
try {
Field cthf = TextInputLayout.class.getDeclaredField("mErrorView");
cthf.setAccessible(true);
TextView error = (TextView) cthf.get(this);
if (error != null)
error.setTypeface(tf);
} catch (Exception e) {
}
}
}
This is how i achieve this
edit_login_emailOrPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
{
textInputLayout_login_emailOrPhone.setTypeface(APSApplication.getInstance().getFonts().getTypefaceSemiBold());
}else
{
textInputLayout_login_emailOrPhone.setTypeface(APSApplication.getInstance().getFonts().getTypefaceRegular());
}
}
});
In case you too met an exotic requirement to set custom font ONLY to the floating label, and anything else didn't work for you as well, try this. This worked for me, at least for material lib ver. 1.3.0-alpha03.
#SuppressLint("RestrictedApi")
fun setHintFontFamily(view: TextInputLayout, fontRes: Int) {
val font = ResourcesCompat.getFont(view.context, fontRes)!!
try {
val collapsingTextHelperField =
view::class.java.getDeclaredField("collapsingTextHelper").apply {
isAccessible = true
}
val collapsingTextHelper = collapsingTextHelperField.get(view) as CollapsingTextHelper
collapsingTextHelper.collapsedTypeface = font
} catch (e: Exception) {
}
}
First we get the CollapsingTextHelper as in some other answers, but then we use its property collapsedTypeface that seems to do exactly what we need -- apply a font only to the floating label. Please note that this property's visibility is restricted to library group (that's why I used #SuppressLint). So the implementation details might change in the future.