How do I set a fixed minimum EditText width so that it snugly flows around typed and limited-length typed content?

I want to set a minimum fixed width for EditText, so that it can contain its tooltip, but also typed, length-limited content, such as a two-digit number.

Some information:

  • I want to be able to do this dynamically, since I have many fields for different purposes with different hints (in different languages) and input length (some 2 digits, others 4).
  • Hints are not necessarily longer than the input itself. the prompt may be “dd” or “Day,” and the input may be a digit number.
  • I do not need a place for hint and content at the same time; tooltips disappear when the user starts typing.
  • I use custom fonts in the extended class EditText, but this needs to be handled when I copy EditText Paint.

I have a utility method for this, but it returns a width that is too narrow , so the tooltip is cropped. What am I doing wrong?

EditText text is specified in XML as follows:

 <EditText
        android:id="@+id/birthday_month"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:hint="@string/birthday_month_hint"
        android:lines="1"
        android:maxLength="2">

In my work, I will first find EditText, and then prepare it using the Texts.setNiceAndTightWidth(monthEditText, 2)one defined below (including helper methods):

public class Texts
{

    public static void setNiceAndTightWidth ( EditText editText, int maxInputLength )
    {
        // String of chars to test for widest char.  Include all possible input chars and chars of hint, as we need to make room for hint as well.
        String testChars = String.format("1234568790%s", editText.getHint().toString());

        char widestChar = getWidestChar(editText, testChars);
        String widestString = repeat(widestChar, maxInputLength);

        float widestStringWidth = getTextWidth(editText, widestString);
        int width = (int)(widestStringWidth + 0.5f);

        editText.setWidth(width);

        // This was an experiment but it doesn't help.
        ViewGroup.LayoutParams lp = editText.getLayoutParams();
        lp.width = width;
        editText.setLayoutParams(lp);
    }


    public static char getWidestChar ( TextView textView, String testChars )
    {
        float width, widest = 0;
        char widestChar = '\0';

        // Using Paint properties of TextView, including Fontface, text size, etc.
        Paint paint = new Paint( textView.getPaint() );

        for ( int i = 0 ; i < testChars.length() ; i++ ) {
            width = paint.measureText(testChars, i, i+1);
            if ( width > widest ) {
                widest = width;
                widestChar = testChars.charAt(i);
            }
        }

        return widestChar;
    }

    public static String repeat ( char ch, int length )
    {
        char[] chars = new char[length];
        Arrays.fill(chars, ch);
        String string = String.valueOf(chars);
        return string;
    }

    public static float getTextWidth ( TextView textView, CharSequence text )
    {
        Paint paint = new Paint( textView.getPaint() );
        float width = paint.measureText(text, 0, text.length());
        return width;
    }

}
+4
source share

Source: https://habr.com/ru/post/1667758/


All Articles