How to reduce EditText
Hint size?
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
@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.
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);
}
}
});
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);
@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.
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)
}
}
}
}