Reputation: 1201
I am currently trying to fix my problem with string length at buttons, in android application. So i parse string and then set it as button text. But if one text is bigger then 9 characters or 10 it gets displayed out of the button overlay. I know i could make text smaller but this is not good solution because i already have small text.
So what you guys recommend for example if i have:
String text = "ThisIsSomeRealyLongText";
How can i split this string in 2 lines or when i reach character number 9 just use /n (to break text)?
Upvotes: 0
Views: 67
Reputation: 7546
Use
(new line):
android:text = "ThisIsSome RealyLongText"
But in code, you can just use \n
. You can define in ../res/values/strings.xml
:
<string name="longname">ThisIsSome\nRealyLongText</string>
then set the text for you button
android:text="@string/longname"
Upvotes: 1
Reputation: 4843
Using too much text on a button is never a good idea. You should think about using an icon, or shorter text with (if needed) more explanation text somewhere else. Remember,the best UX will be the one with the least amount of reading involved.
Upvotes: 2
Reputation: 39355
Using regex you can do this. It will break the string into 9 characters per line.
text = text.replaceAll("(.{9})", "$1\n");
Upvotes: 1