The size of the regular hint text is set to the EditText
's text size when that is added to the TextInputLayout
during inflation/initialization. This value is ultimately set on a private helper class in TextInputLayout
, and there is no publicly exposed method or field to change it.
However, we can do a little juggling with the text sizes by subclassing TextInputLayout
to intercept the adding of the EditText
. When the EditText
is added, we cache its text size, set the desired hint size as its text size, allow the super class to add it and initialize the hint, and finally set the EditText
's text size back to its original value.
For example:
public class CustomTextInputLayout extends TextInputLayout {
private float mainHintTextSize;
private float editTextSize;
public CustomTextInputLayout(Context context) {
this(context, null);
}
public CustomTextInputLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomTextInputLayout(Context context, 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(View child, int index, ViewGroup.LayoutParams params) {
final boolean b = child instanceof EditText && mainHintTextSize > 0;
if (b) {
final EditText e = (EditText) child;
editTextSize = e.getTextSize();
e.setTextSize(TypedValue.COMPLEX_UNIT_PX, mainHintTextSize);
}
super.addView(child, index, params);
if (b) {
getEditText().setTextSize(TypedValue.COMPLEX_UNIT_PX, editTextSize);
}
}
// Units are pixels.
public float getMainHintTextSize() {
return mainHintTextSize;
}
// This optional method allows for dynamic instantiation of this class and
// its EditText, but it cannot be used after the EditText has been added.
// Units are scaled pixels.
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());
}
}
To use the custom mainHintTextSize
attribute, we'll need the following in our <resources>
, which we can do by just sticking the following file in the res/values/
folder, or adding to the one that's already there.
attrs.xml
<resources>
<declare-styleable name="CustomTextInputLayout" >
<attr name="mainHintTextSize" format="dimension" />
</declare-styleable>
</resources>
If you don't care to use the custom attribute, you can skip this file, and remove the TypedArray
processing in the third constructor above.
This custom class is a drop-in replacement for TextInputLayout
, and can be used just as it would. For example:
<com.mycompany.myapp.CustomTextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
app:hintTextAppearance="@style/TextLabel"
app:mainHintTextSize="12sp">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="qwerty123" />
</com.mycompany.myapp.CustomTextInputLayout>
This approach is nice, in that it uses only publicly-accessible, documented methods, but the hint text size must be set before the EditText
is added, whether that happens during inflation, or through direct instantiation.