Android Edittext Hint Size

How to change the size of the EditText hint in TextInputLayout

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>

screenshot


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.

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

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) {
}
});

How to change hint text size in TextInputLayout

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.

How to change the hint size of TextInputLayout

In your styles.xml

<style name="TextLabel" parent="TextAppearance.Design.Hint">
<item name="android:textSize">16sp</item>
</style>

And then in your layout file:

<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="12dp"
app:hintTextAppearance="@style/TextLabel"
android:minHeight="30dp">

TextInputLayout's floating hint text size

You can do like this.

change EditText's text size

android:textSize="12sp"

You can do this in the .java.

editText.setTextSize(12);

change TextInputLayout's LEFT-TOP text size

app:hintTextAppearance="@style/text_in_layout_hint_Style"

style code

<style name="text_in_layout_hint_Style">
<item name="android:textColor">#FF0000</item>
<item name="android:textSize">20sp</item>
</style>

You can do this in .java.

textInputLayout.setHintTextAppearance(R.style.text_in_layout_hint_Style);

Hint size of an EditText in XML

I am not aware of any way in which you can accomplish this through xml only (apart from sticking html tags in the hint text).

But there is another way that was not outlined above, that would allow you to keep the font size in sp and separate from the java code:

final int hintSize = <read_this_from_xml_resources_but_take_into_account_density>;
final int textSize = <read_this_from_xml_resources_but_take_into_account_density>;
editText.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {
}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}

@Override
public void onTextChanged(CharSequence arg0, int start, int before, int count) {
editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, arg0.length() > 0 ? textSize : hintSize);
}
});


Related Topics



Leave a reply



Submit