Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change hint text size without changing the text size in EditText

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" />  
like image 946
dev90 Avatar asked Aug 23 '16 17:08

dev90


People also ask

How do I change text hint size?

fromHtml( "<small><small><small>" + getString(R. string. hint) + "</small></small></small>")); Also, you can set the size in the string resource file where is the string for the hint.

How do I change TextInputLayout size on android?

You only need to change the android:textSize attribute for the TextInputEditText for the hint size to change. Save this answer. Show activity on this post. The problem is that i whant to make edit text size biger and hint smaller.


2 Answers

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"
like image 181
Filip Rosca Avatar answered Oct 19 '22 19:10

Filip Rosca


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) {
    }
});
like image 29
earthw0rmjim Avatar answered Oct 19 '22 18:10

earthw0rmjim