92

How to reduce EditText Hint size?

GabrielOshiro
  • 7,986
  • 4
  • 45
  • 57
Sasikumar
  • 1,043
  • 1
  • 8
  • 9

13 Answers13

140

You can do it by setting a size in the string recource.

For example:

<string name="edittext_hint"><font size="15">Hint here!</font></string>

then in your XML just write

android:hint="@string/edittext_hint"

This will resault in a smaller text for the hint but the original size for the input text.

Hopes this helps for future readers

Jeka
  • 1,514
  • 1
  • 10
  • 6
61

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

Ali
  • 9,800
  • 19
  • 72
  • 152
inky
  • 1,462
  • 14
  • 17
38

I also had to do this as my hint didn't fit in the EditText at the standard size. So I did this (in the xml set textSize to mHintTextSize):

MYEditText.addTextChangedListener(new TextWatcher(){

                @Override
                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1,
                        int arg2, int arg3) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onTextChanged(CharSequence arg0, int start, int before,
                        int count) {
                    if (arg0.length() == 0) { 
                        // No entered text so will show hint
                        editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mHintTextSize);
                    } else {
                        editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mRealTextSize);
                    }
                }
        });
Ion Aalbers
  • 7,830
  • 3
  • 37
  • 50
longhairedsi
  • 3,133
  • 2
  • 28
  • 28
  • 2
    @EVSearchTerm is his edittext reference. naming convention forsaken. – Rohit Sharma Nov 17 '14 at 12:47
  • 1
    This approach works. The problem of the Android SDK here is that setting text size programmatically is completely ignored for hint text size. On the other side, this allows longhairedsi's approach to be simplified. Instead of adding a `TextWatcher` you can simply call `editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mRealTextSize);` immediately. However, longhairedsi's solution might be more future safe. I'm curious if this issue will be fixed in newer SDK versions. – Peter F Sep 06 '16 at 14:33
  • Thanks. Don't forget to set initial text size (otherwise when after start a text is empty, hint's size maybe equal to text size). – CoolMind Jul 23 '18 at 12:56
31

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)

Community
  • 1
  • 1
marmor
  • 27,641
  • 11
  • 107
  • 150
13

If you want to do it programmatically,

SpannableString span = new SpannableString(strHint);
span.setSpan(new RelativeSizeSpan(0.5f), 0, strHint.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setHint(span);
Sora Takayama
  • 139
  • 1
  • 3
  • Loved your solution but unfortunately didn't work, loading from resources and setting it dynamically to TextInputLayout – Jamal S Jan 05 '20 at 11:51
10

it is easy to reduce the hint size of the edittext

editText.setHint(Html.fromHtml(
    "<font size=\"5\">" + "hinttext1" + "</font>" + 
    "<small>" + "hinttext2" + "</small>" )); 
Inverce
  • 1,487
  • 13
  • 27
ManiTeja
  • 849
  • 1
  • 9
  • 13
3

define this in your strings.xml in values folder :

<string name="enter_otp"><font size="16">your text</font></string>
Mohammad Davari
  • 430
  • 3
  • 13
2

@marmor 's Approach is the best One. You can alter the number of <small> --- </small>tags to adjust size.

You can also define the text of Hint directly as I did

view.setHint(Html.fromHtml("<small><small><small>" + "This is Hint" + "</small></small></small>"));

Hope this will help.

dmSherazi
  • 3,743
  • 5
  • 37
  • 62
1

@user2982553 's solution works great for me. You could also use AbsoluteSizeSpan, with which you can set the exact font size of the hint. Don't use <font size=\"5\"> tag, 'cause the size attribute is just ignored.

trojantale
  • 196
  • 1
  • 7
1

You just need to use "app:hintTextAppearance="@style/TextInputLayoutHintText" to your TextInputEditText, where in style keep whatever size you required

e.g.: <style name="TextInputLayoutHintText"> <item name="android:textSize">@dimen/text_size_7</item> </style>

Gaurav Karia
  • 166
  • 8
0

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;
    }
}
xuxu
  • 6,374
  • 1
  • 17
  • 11
0

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);
Ayaz Alifov
  • 8,334
  • 4
  • 61
  • 56
0

Using onFocusChanged() listener for changing hint font size is also an option, as addTextChangeListener() won't trigger when user clicks on text field, and blinking cursor will resize to hint font.

Also, unlike with TextChangeListener, there is no need to set initial hint font size separately.

class EditTextWithHintSize {
 init {
        val typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.EditTextWithHintSize, 0, defStyle)
        try {
            hintFontSize = typedArray.getDimension(R.styleable.EditTextWithHintSize_hint_font_size, textSize)
            fontSize = textSize

            if (length() == 0) {
                setTextSize(TypedValue.COMPLEX_UNIT_PX, hintFontSize)
            }
        } catch (e: Exception) {
            hintFontSize = textSize
            fontSize = textSize
        } finally {
            typedArray.recycle()
        }
    }

    override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect)

        if (focused) {
            setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize)
        } else {
            if (length() == 0) {
                setTextSize(TypedValue.COMPLEX_UNIT_PX, hintFontSize)
            } else {
                setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize)
            }
        }
    }
}
Kurovsky
  • 1,081
  • 10
  • 25