0

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)?

0

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Using regex you can do this. It will break the string into 9 characters per line.

text = text.replaceAll("(.{9})", "$1\n"); 

2 Comments

if the text already has a newline, this won't take that into account.
@RedAlert it doesn't require to break string if a line has less then 9 characters(when newlines are around).
1

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" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.