Categories: java, android, xml, android-layout

Setting Lenght Limit in editText in Java

1 answer

In my Android app, I will take the name of the person with an editText. Then, I will send it to a textView to show that in the subsequent activities. But, I have a limited area. That's why I have to limit the length of the name. In XML, we can restrict the character length with a code called maxLength. However, I don't want to use it because I want to limit the text within the design limits.

I will visualize the reason why I don't want to use this method. Let's imagine that the person wants to use 8-Z(ZZZZZZZZ) as a name; on the other hand, another person wants to use 8-i(iiiiiiii). Of course, their actual length will be different in design. So, I want to limit the name based on its length on design instead of its character length. I'm waiting for your contributions and suggestions.

Received answer to this question:
The best answer according to the author of the question:

You can use one line TextView with scaling the TextSize and fit the whole text in its layout bounds.

<TextView     android:layout_width="match_parent"     android:layout_height="wrap_content"     app:autoSizeTextType="uniform"     app:autoSizeMinTextSize="8dp"     app:autoSizeMaxTextSize="12dp"     app:autoSizeStepGranularity="1dp"      android:singleLine="true"     android:maxLines="1"/>