How to resize tooltip text without resizing text in EditText

I have an input field EditText. I added a hint in this. Now I want to change the size of the tooltip text, but when I do this, it also affects the text size. Please indicate to me how to resize the tooltip and text individually and provide different fonts for both the tooltip 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" />  
+4
source share
4 answers

Tooltip and text are exclusive if one is visible and the other is not.

- EditText , ( ) ( ).

:

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) {
    }
});
+5

.

:

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

XML:

android:hint="@string/hint"
+16

, .

+1

HTML , . , . , :

1) Hint:

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) MetricAffectingSpan:

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) :

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

Source: https://habr.com/ru/post/1652224/


All Articles