2

imagine i got a TextView text1 = findViewById(R.id.text1); - That Textbox is filling parent (Taken up the whole screen width) and is filled with dynamic string. That String can be 6 - 50 Characters, so i want to cut it, but... I want to cut it nicely...

I would want to get the current screen width and then cut the string depending on the screen width.

So now the Question: Is the any function or magic maths, that'd save me a lot of writing, so that i won't have to write down like every single width?

  • Maybe you should look this answer http://stackoverflow.com/questions/4142090/how-do-you-to-retrieve-dimensions-of-a-view-getheight-and-getwidth-always-r/4406090#4406090 – aleksamarkoni Oct 21 '15 at 16:29
  • Find the minimum width required for your text then check the window width programatically then you can be able to do the rest. – Toppers Oct 21 '15 at 16:30
  • Thanks for that answer and sorry, i might not made myself clear there. I know how to get the resolution and width, but instead of writing something like: if (width == 600) {/** Do something**/} if (width == 800 {/** Do something**/} i wondered, if there was a special function or something like that, which would save me all those IF's. – Martin McCormack Oct 21 '15 at 16:35
  • Setting `android:singleLine="true"` and `android:ellipsize="end"` on your `TextView` might also be suitable for your needs – Ken Wolf Oct 21 '15 at 16:35
  • @KenWolf the singleLine did the Trick, thanks. If you wanna do that as an answer i can vote it done. – Martin McCormack Oct 21 '15 at 16:41

1 Answers1

2

Instead of manually detecting the screen width and truncating the string, I would use the native TextView functionality to achieve your goals.

<TextView
    android:singleLine="true"
    android:ellipsize="end" 
    ... 
    />

Setting these two attributes will mean the text will not expand the TextView and if it is longer than the TextView it will truncate instead.

Ken Wolf
  • 23,133
  • 6
  • 63
  • 84