mercredi 13 septembre 2023

Trying to override the font size of label of the TextInputLayout, using reflection

I'm trying to change the font size of the label / hint of TextInputLayout. As it has a property collapsingTextHelper where it holds all data related to font of label / hint, accessing it, would solve the problem. But it's private. So I'm trying to use reflect.

Relavent code:

Field field = TextInputLayout.class.getDeclaredField("collapsingTextHelper");
                field.setAccessible(true);
                Object collapsingTextHelper = field.get(this);

                Field expandedTextSizeField = collapsingTextHelper.getClass().getDeclaredField("expandedTextSize");
                Field collapsedTextSizeField = collapsingTextHelper.getClass().getDeclaredField("collapsedTextSize");

                expandedTextSizeField.setAccessible(true);
                expandedTextSizeField.setFloat(collapsingTextHelper, mainHintTextSize);
                collapsedTextSizeField.setAccessible(true);
                collapsedTextSizeField.setFloat(collapsingTextHelper, mainHintTextSize);

Here is my full code:


public class CustomTextInputLayout extends TextInputLayout {
    private final float mainHintTextSize = 24;

    public CustomTextInputLayout(@NonNull Context context) {
        super(context);
    }

    public CustomTextInputLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomTextInputLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTextInputLayout);
//        mainHintTextSize = a.getDimensionPixelSize(R.styleable.CustomTextInputLayout_mainHintTextSize, 0);
        a.recycle();
    }

    @Override
    public void addView(@NonNull View child, int index, @NonNull ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        if (child instanceof EditText) {
            try {
                Field field = TextInputLayout.class.getDeclaredField("collapsingTextHelper");
                field.setAccessible(true);
                Object collapsingTextHelper = field.get(this);

                Field expandedTextSizeField = collapsingTextHelper.getClass().getDeclaredField("expandedTextSize");
                Field collapsedTextSizeField = collapsingTextHelper.getClass().getDeclaredField("collapsedTextSize");

                expandedTextSizeField.setAccessible(true);
                expandedTextSizeField.setFloat(collapsingTextHelper, mainHintTextSize);
                collapsedTextSizeField.setAccessible(true);
                collapsedTextSizeField.setFloat(collapsingTextHelper, mainHintTextSize);

            } catch (NoSuchFieldException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    public float getMainHintTextSize() {
        return mainHintTextSize;
    }

    public void setMainHintTextSize(float size) {
        if (getEditText() != null) {
            throw new IllegalStateException("Hint text size must be set before EditText is added");
        }

//        mainHintTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, size, getResources().getDisplayMetrics());
    }
}

And it doesn't raise any error.

But even the code has been executed, it doesn't change the font size of the collapsed text.





Aucun commentaire:

Enregistrer un commentaire