0

I have a combination of strings and depending on the maximum number of characters that will fit on the button I want to adapt the length of the individual strings. How do I get the max number of characters on a button:

Button foo=(Button)findViewById(R.id.foobutton);
String a="longtext";
String b="longtext";
String c="longtext";
foo.setText(a+b+c);

But if a+b+c won't fit, I want to cut off for example String b.

I tried the following, but it gives more characters than actually fit:

 int maxpix=foo.getLayout().getEllipsizedWidth();

          float textlengthinpix=foo.getPaint().measureText(a+b+c);
          float pixperchar=textlengthinpix/((a+b+c).length());
          int maxcharsinview=(int) (maxchars/pixperchar);
Diego
  • 4,011
  • 10
  • 50
  • 76
  • [How to get string width on Android](http://stackoverflow.com/questions/3630086/how-to-get-string-width-on-android) will help since then you'll know if it fits or not. –  Nov 04 '14 at 03:53
  • By default the button will grow to accommodate all the text, how do you determine if it won't fit? If the button width is bigger than the parent view width? – momo Nov 04 '14 at 03:56
  • Getting the size and wrapping point of any given laid out text is one of the best kept secrets in the Android SDK. I wish Google gave more access to lower level APIs for text rendering *ahem* OGL *ahem*. – MLProgrammer-CiM Nov 04 '14 at 03:58
  • @MLProgrammer-CiM, are you saying I can't get the maximum number of characters that would fit on a button if, like momo said, the text will be bigger than the parent view width. – Diego Nov 04 '14 at 04:05
  • I haven't touched this for a while, but this doc page is a good starting point http://developer.android.com/reference/android/text/Layout.html – MLProgrammer-CiM Nov 04 '14 at 04:07
  • Related: http://developer.android.com/reference/android/widget/TextView.html#getPaint() – MLProgrammer-CiM Nov 04 '14 at 04:08
  • Related too: http://developer.android.com/reference/android/widget/TextView.html#getLayout() – MLProgrammer-CiM Nov 04 '14 at 04:10
  • So, I'm guessing foo.getLayout().getEllipsizedWidth() returns the pixels, since it's 243. How can I translate those pixels to actual characters in a String? – Diego Nov 04 '14 at 04:17
  • http://developer.android.com/reference/android/text/TextUtils.html ellipsize methods with the paint from the button – MLProgrammer-CiM Nov 04 '14 at 04:19
  • Please confirm it was like that so I can fav this question and never forget the answer. – MLProgrammer-CiM Nov 04 '14 at 04:28
  • I guess I can check whether it fits or not: Returns the original text if it fits in the specified width given the properties of the specified Paint, or, if it does not fit, a truncated copy with ellipsis character added at the specified edge or center - but if it doesn't fit, I still don't know how many chars would fit - or am I wrong? – Diego Nov 04 '14 at 04:38

2 Answers2

0

try adding

android:maxLength="10" //say max length set to 10 character

property with the button widget...

Angad Tiwari
  • 1,738
  • 1
  • 12
  • 23
  • Thing is, I don't want to limit the length, I want to use the maximum number of chars and adapt my string accordingly – Diego Nov 05 '14 at 17:30
0

I managed to do it as follows:

int maxcharspix=0;
float textlengthinpix=0;
float pixperchar=0;
String longstring="verylongstring";
try {
  maxcharspix=btn.getLayout().getEllipsizedWidth();
  textlengthinpix=btn.getPaint().measureText(longstring)*btn.getPaint().getTextScaleX();
  pixperchar=textlengthinpix/(longstring.length());

} catch (Exception e){
  e.printStackTrace();
}

int maxcharsinview=(int) (maxcharspix/pixperchar);

if (maxcharsinview<btn.length()){
//string is too long for button              
}

btn.setText(adaptedlongstring);
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Diego
  • 4,011
  • 10
  • 50
  • 76