Tooltip size for Android EditText

How to reduce size of EditText Hint?

+66
android
Jun 29 '10 at 10:09
source share
11 answers

You can do this by setting the size in the source text of the string.

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 result in the rejection of the smaller text for the tooltip, but the original size for the text input.

Hope this helps future readers.

+110
Apr 30 '15 at 9:28
source share

You can reduce the font size on EditText , which will also reduce the hint size. those. android:textSize="16sp"

+53
Jun 24 '11 at 20:14
source share

I also had to do this, since my hint did not match the EditText of the standard size. So I did this (in xml set textSize for 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); } } }); 
+35
Aug 26 2018-11-11T00:
source share

You can set simple HTML attributes for the tooltip itself.

See accepted answer here: Android EditText tooltip

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

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

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

+31
Apr 30 '12 at 15:35
source share

If you want to do this programmatically,

 SpannableString span = new SpannableString(strHint); span.setSpan(new RelativeSizeSpan(0.5f), 0, strHint.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); editText.setHint(span); 
+9
Nov 12 '13 at 9:28
source share

easily reduce edittext tooltip size

 editText.setHint(Html.fromHtml( "<font size=\"5\">" + "hinttext1" + "</font>" + "<small>" + "hinttext2" + "</small>" )); 
+8
Apr 17 '13 at 13:28
source share

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

You can also define the hint text directly, as I did

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

Hope this helps.

+1
Nov 04 '13 at 18:36
source share

@ user2982553 solution works fine for me. You can also use AbsoluteSizeSpan , with which you can set the exact font size of the tooltip. Do not use the <font size=\"5\"> because the size attribute is simply ignored.

+1
Jan 16 '14 at 9:32
source share

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

 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
Jan 24 '16 at 19:16
source share

You can change not only the size of the tooltip, but also the font and style. I decided 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 your own 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
Aug 2 '17 at 9:28
source share

Using the onFocusChanged () listener to change the font size of the tooltip is also an option, since addTextChangeListener () will not work when the user clicks on the text field, and the blinking cursor changes the font size of the tooltip.

In addition, unlike TextChangeListener, there is no need to set the initial font size of the tooltip 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) } } } } 
0
Jun 25 '19 at 14:12
source share



All Articles