I need to make sure the input string can fit the line where I'm going to show it. I already know how to limit the number of characters but that is not very good because 2 strings with the same character length have differente size... For exemple:
String1: "wwwwwwwwww"
String2: "iiiiiiiiii"
in android string1 is much larger than string 2 because "i" consumes less visual space than "w"
You can use
TextWatcher
to analyse text being entered andPaint
to measure the width of the current value of the text.This is a code I used in TextWatcher's function afterTextChanged for this purpose. I based the solution on Asahi's advice. I'm no pro programmer so the code looks probably awful. Feel free to edit it to make it better.
Just a small advice if you want to change the fontSize already when loading the activity. It won't work if you use the function in OnCreate method because at this point the UI isn't defined yet so you need to use this function to get the desired result.