17

I have a EditText input field. I have added a hint in it. Now i want to change the size of hint text, but when i do this, it also effects the size of the text. Kindly guide me how to change the size of hint and text separately, and give different fonts to both the hint and the text.

<EditText
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:layout_weight="1"
    android:textSize="12sp"
    android:textColor="#ffffff"                            
    android:fontFamily="sans-serif-light"
    android:hint="MM/YY"
    android:textColorHint="@color/white" />  
earthw0rmjim
  • 19,027
  • 9
  • 49
  • 63
dev90
  • 7,187
  • 15
  • 80
  • 153

4 Answers4

48

You can set it in resource file.

For example:

<string name="hint"><font size="20">Hint!</font></string>

And your XML:

android:hint="@string/hint"
Filip Rosca
  • 601
  • 1
  • 6
  • 13
  • 1
    can i add `Font` there as well? – dev90 Aug 23 '16 at 17:28
  • Yes, you can, you also can specify the color of text there, for instance. – Filip Rosca Aug 23 '16 at 17:33
  • 1
    I think, it is easiest way to resolve this. It takes many line's of code if you want to handle it in java code. – Filip Rosca Aug 23 '16 at 17:40
  • Yes, Can you guide me how to add Color and Font Name in this – dev90 Aug 23 '16 at 18:19
  • You should add string resource to values/strings.xml file. You can specify text color in the same way: Hello World! – Filip Rosca Aug 23 '16 at 19:16
  • I made a mistake, try this: Hello world! It should work, I checked. – Filip Rosca Aug 23 '16 at 22:17
  • @FilipRosca Please explain how do you change the font with this method. Not just the color but the actual font. – earthw0rmjim Aug 24 '16 at 06:42
  • @user13 If I've got it correctly, you can specify text font in this way: Hello world! - for Italic or Hello world! - for bold, for example. – Filip Rosca Aug 24 '16 at 07:33
  • spent years in Android Development and never knew about this. – iCantC Jan 16 '20 at 13:27
  • 1
    @iCantC i think this is a lie, it did not work when i tested it and i have never seen this on the official documentation – hvar90 Aug 25 '20 at 05:57
  • For me it works if I change part of a string's size for a textview but doesn't change the hint's size. – Itay Feldman Jan 11 '21 at 00:03
  • Just tested this and it worked for me. However I prefer dynamically changing the size based on an empty/populated edit text. It's too easy to forget about changing the font size in strings.xml, or it may cause pain for someone else to debug further down the line. – Andrew Apr 09 '21 at 17:14
13

The hint and the text are exclusive, if one of them is visible, the other one is not.

Because of this, you could just change the attributes of your EditText depending on if it's empty (the hint is visible) or not (the text is visible).

For example:

final EditText editText = (EditText) findViewById(R.id.yourEditText);

editText.addTextChangedListener(new TextWatcher() {
    boolean hint;

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if(s.length() == 0) {
            // no text, hint is visible
            hint = true;
            editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
            editText.setTypeface(Typeface.createFromAsset(getAssets(),
                "hintFont.ttf")); // setting the font
        } else if(hint) {
            // no hint, text is visible
            hint = false;
            editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
            editText.setTypeface(Typeface.createFromAsset(getAssets(),
                "textFont.ttf")); // setting the font
        }
    }

    @Override
    public void afterTextChanged(Editable s) {
    }
});
earthw0rmjim
  • 19,027
  • 9
  • 49
  • 63
3

Using HTML is ok, but it is not flexible. For example, you cannot set the exact size. I will provide an alternative solution where you can set:

  • new Hint font
  • new Hint size
  • new Hint style

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
1

You could set the text size to the smaller, desired value, then set a text listener to change the text size after some text is entered.

Jesse Buss
  • 833
  • 6
  • 13