Pleshkov Ivan
Pleshkov Ivan

Reputation: 151

How to change hint text size in TextInputLayout

Suppose I want to change text size. I'm doing it in code and it looks like this:

_textInputLayout.EditText.SetTextSize(Android.Util.ComplexUnitType.Dip, 40);

When I write text in the entry it looks like 40dip text. But when entry is empty hint text looks like 16-18dip.

Is there any way to change hint text size?

Upvotes: 4

Views: 8101

Answers (2)

Pete
Pete

Reputation: 4746

Changing the final hint size / floating label size is possible via a style and calling SetHintTextAppearance using something like the following:-

_nativeView.SetHintTextAppearance(App6.Droid.Resource.Style.MyTextInputLayout);

Where MyTextInputLayout is something along the lines of:-

<style name="MyTextInputLayout" parent="@android:style/TextAppearance">
    <item name="android:textColor">@color/blue</item>
    <item name="android:textSize">44sp</item>
</style>

However the textSize from this style is only applied to the final destination, when you start to enter some text in.

From what I can see, and including the properties on the object, it doesn't appear to be possible to change the starting font size of the hint unfortunately at the moment?

Where as EditText is exposed, and you can alter things there. The Hint portion is not handled at all by it, and instead by the TextInputLayout. There appears no object exposed to get access to customize this specifically for the Hint.

Upvotes: 5

Stanojkovic
Stanojkovic

Reputation: 1632

You can do it by setting a size in the string recource.

For example:

<string name="edittext_hint"><font size="15">Hint here!</font></string>

then in your XML just write

android:hint="@string/edittext_hint"

This will resault in a smaller text for the hint but the original size for the input text.

Or like this:

MYEditText.addTextChangedListener(new TextWatcher(){

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTextChanged(CharSequence arg0, int start, int before,
                    int count) {
                if (arg0.length() == 0) { 
                    // No entered text so will show hint
                    editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mHintTextSize);
                } else {
                    editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mRealTextSize);
                }
            }
    });

Upvotes: 2

Related Questions