1

I am trying to change hint text size programmatically, but I just can't find the right method. I'm using setHintTextAppearance, like it's shown in example, but it works only when input is focused or filled with some data. I tried to set EditText textSize also, but still no luck.

textInputLayout.setHintTextAppearance(Vabaco_TextInputLayout_hint_small);
EditText a = textInputLayout.getEditText();
a.setTextSize(8);
Leo Odishvili
  • 1,004
  • 1
  • 7
  • 29

3 Answers3

2

You can change hint text size when it unfocused using reflection like this;

 try {
        Field filed = TextInputLayout.class.getDeclaredField("mCollapsingTextHelper");
        filed.setAccessible(true);
        Object helper = filed.get(textInputLayout);

        Field f1 = helper.getClass().getDeclaredField("mExpandedTextSize");
        f1.setAccessible(true);

        f1.set(helper,100);
    }
    catch (NoSuchFieldException | IllegalAccessException e) {
        e.printStackTrace();
    }

name of mExpandedTextSize may be different according to the dependency version for TextInputLayout. You should check TextInputLayout and CollapsingTextHelper classes for the name of variables.

Hope this helps you.

felix
  • 36
  • 1
  • 3
  • It actually worked! Thanks. The only question is, does f1.set(), second parameter user percent? Because it's for sure not a points. Sorry for late response, though. Had a hard time at job. – Leo Odishvili Aug 14 '19 at 06:32
  • Actually, I gave a random number but you can give this size like [this](https://stackoverflow.com/questions/8295986/how-to-calculate-dp-from-pixels-in-android-programmatically/19953871#19953871) @LeoOdishvili – felix Aug 15 '19 at 13:02
1

Reflection solution doesn't work on support:design:28.0.0(mExpandedTextSize-> expandedTextSize). Also, Android Q (and later) doesn't support some non-sdk solutions.

Create your custom layout:

public  class CustomTextInputLayout extends TextInputLayout {
    public CustomTextInputLayout(Context context) {
        super(context);
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        if(child instanceof EditText) {
            ((EditText)child).setTextSize(16);
        }

        super.addView(child, index, params);
    }
}
zakjma
  • 2,030
  • 12
  • 40
  • 81
0

If setting the text size programmatically is not required you can try like below,I have disabled TextInputLayout hint,

<android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:hintEnabled="false">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/edittext"
                android:hint="yorhint"
                android:inputType="text"
                android:textColorHint="@color/colorLightBlack"
                android:textSize="10sp" />
        </android.support.design.widget.TextInputLayout>

If required programmatically you can find edittext by id and set the text size.

Padmini S
  • 143
  • 1
  • 10