Android EditText Hint Size

前端 未结 12 1064
轮回少年
轮回少年 2020-11-28 06:21

How to reduce EditText Hint size?

相关标签:
12条回答
  • 2020-11-28 06:55

    define this in your strings.xml in values folder :

    <string name="enter_otp"><font size="16">your text</font></string>
    
    0 讨论(0)
  • 2020-11-28 06:55

    You can change not only a hint's size but also its font and style. I achieved to solve it using SpannableString and MetricAffectingSpan

    1) Create a custom Hint object:

    import android.graphics.Typeface;
    import android.text.SpannableString;
    import android.text.Spanned;
    import android.text.style.MetricAffectingSpan;
    
    public class CustomHint extends SpannableString
    {
        public CustomHint(final CharSequence source, final int style)
        {
            this(null, source, style, null);
        }
    
        public CustomHint(final CharSequence source, final Float size)
        {
            this(null, source, size);
        }
    
        public CustomHint(final CharSequence source, final int style, final Float size)
        {
            this(null, source, style, size);
        }
    
        public CustomHint(final Typeface typeface, final CharSequence source, final int style)
        {
            this(typeface, source, style, null);
        }
    
        public CustomHint(final Typeface typeface, final CharSequence source, final Float size)
        {
            this(typeface, source, null, size);
        }
    
        public CustomHint(final Typeface typeface, final CharSequence source, final Integer style, final Float size)
        {
            super(source);
    
            MetricAffectingSpan typefaceSpan = new CustomMetricAffectingSpan(typeface, style, size);
            setSpan(typefaceSpan, 0, source.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
    }
    

    2) Create custom MetricAffectingSpan object:

    import android.graphics.Typeface;
    import android.text.TextPaint;
    import android.text.style.MetricAffectingSpan;
    
    public class CustomMetricAffectingSpan extends MetricAffectingSpan
    {
        private final Typeface _typeface;
        private final Float    _newSize;
        private final Integer  _newStyle;
    
        public CustomMetricAffectingSpan(Float size)
        {
            this(null, null, size);
        }
    
        public CustomMetricAffectingSpan(Float size, Integer style)
        {
            this(null, style, size);
        }
    
        public CustomMetricAffectingSpan(Typeface type, Integer style, Float size)
        {
            this._typeface = type;
            this._newStyle = style;
            this._newSize = size;
        }
    
        @Override
        public void updateDrawState(TextPaint ds)
        {
            applyNewSize(ds);
        }
    
        @Override
        public void updateMeasureState(TextPaint paint)
        {
            applyNewSize(paint);
        }
    
        private void applyNewSize(TextPaint paint)
        {
            if (this._newStyle != null)
                paint.setTypeface(Typeface.create(this._typeface, this._newStyle));
            else
                paint.setTypeface(this._typeface);
    
            if (this._newSize != null)
                paint.setTextSize(this._newSize);
        }
    }
    

    3) Use:

    Typeface newTypeface = Typeface.createFromAsset(getAssets(), "AguafinaScript-Regular.ttf");
    CustomHint customHint = new CustomHint(newTypeface, "Enter some text", Typeface.BOLD_ITALIC, 60f);
            //        CustomHint customHint = new CustomHint(newTypeface, "Enter some text", Typeface.BOLD_ITALIC);
            //        CustomHint customHint = new CustomHint(newTypeface, "Enter some text", 60f);
            //        CustomHint customHint = new CustomHint("Enter some text", Typeface.BOLD_ITALIC, 60f);
            //        CustomHint customHint = new CustomHint("Enter some text", Typeface.BOLD_ITALIC);
            //        CustomHint customHint = new CustomHint("Enter some text", 60f);
    
    customEditText.setHint(customHint);
    
    0 讨论(0)
  • 2020-11-28 06:57

    it is easy to reduce the hint size of the edittext

    editText.setHint(Html.fromHtml(
        "<font size=\"5\">" + "hinttext1" + "</font>" + 
        "<small>" + "hinttext2" + "</small>" )); 
    
    0 讨论(0)
  • 2020-11-28 06:57

    I need to set a larger size for real text than hint.

    public static class LargeSizeTextWatcher implements TextWatcher {
    
        private final EditText mEditText;
        private final int mOriginalSize;
        private final int mLargeSize;
    
        private int mLastLength;
    
        TrackingNumberTextWatcher(EditText editText) {
            mEditText = editText;
            mOriginalSize = (int) editText.getTextSize();
            mLargeSize = editText.getResources().getDimensionPixelSize(R.dimen.text_size_large);
    
            mLastLength = editText.length();
            if (mLastLength != 0) {
                mEditText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mLargeSize);
            }
        }
    
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
    
        @Override
        public void afterTextChanged(Editable s) {
            int length = s.length();
            if (length == 0) {
                mEditText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mOriginalSize);
            } else if (mLastLength == 0) {
                mEditText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mLargeSize);
            }
            mLastLength = length;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 07:03

    You can set simple HTML attributes to the hint string itself.

    See accepted answer here: Android EditText hint

    EDIT: just played with it myself, this worked for me:

    view.setHint(Html.fromHtml("<small><small><small>" + 
                 getString(R.string.hint) + "</small></small></small>"));
    

    This is a list of tags accepted by fromHtml: http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html (though didn't work for me)

    0 讨论(0)
  • 2020-11-28 07:13

    You can reduce the font size on the EditText - that will reduce the size of the hint as well. i.e. android:textSize="16sp"

    0 讨论(0)
提交回复
热议问题