How to set the width of a TextInputLayout depending on the hint length?

Viewed 284

I have a TextInputLayout with a dynamic hint. The length of the hint is between 6-10 characters and the length of the input is always smaller (3-5 characters).

Because the length of the Hint string will be different I want to set the layout_width to depend on the length of the hint.

If I just set android:layout_width="wrap_content" it will take the width of the input, not the length of the hint.

enter image description here

This is my code so far:

    <android.support.design.widget.TextInputLayout
        android:layout_width="95dp"
        android:layout_height="wrap_content"
        android:background="@drawable/textinput_gray_alt"
        local:hintTextAppearance="@style/TextAppearance.App.TextInputLayout">
    <android.support.design.widget.TextInputEditText
        android:id="@+id/quantityEditFirstValue"
        android:fontFamily="@drawable/RobotoMedium"
        android:textSize="16sp"
        android:background="@null"
        android:textColor="@color/tundora"
        android:inputType="numberDecimal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:maxLength="7"
        local:MvxBind="Text FirstQuantityValue; Hint FirstQuantity.Unit; TextColor NativeColor(FirstQuantityTextColor)"/>
</android.support.design.widget.TextInputLayout>
1 Answers

Did you try to do like this:

TextInputEditText textInputEditText = FindViewById<TextInputEditText>(Resource.Id.quantityEditFirstValue);
textInputEditText.Hint = "xxxxxxxx";
var measureText = (int)textInputEditText.Paint.MeasureText(textInputEditText.Hint);
textInputEditText.SetWidth(textInputEditText.PaddingLeft + textInputEditText.PaddingRight + measureText);
Related