Change font of the floating label EditText and TextInputLayout

前端 未结 12 935
北海茫月
北海茫月 2020-12-03 01:23

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 h

相关标签:
12条回答
  • 2020-12-03 01:27

    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:

    0 讨论(0)
  • 2020-12-03 01:30

    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

    0 讨论(0)
  • 2020-12-03 01:31

    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());
                }
            }
        });
    
    0 讨论(0)
  • 2020-12-03 01:33

    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) {
    
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-03 01:36

    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.

    0 讨论(0)
  • 2020-12-03 01:36
    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);
    
    0 讨论(0)
提交回复
热议问题