views:

36

answers:

1

I have inflated a View into a PopupWindow. This View includes a TextView where I want to place text at runtime. I need to count the number of lines the TextView uses in order to adjust the size of the pop-up. For this purpose I use TextView's getLineCount() and draw the TextView before counting the number of lines ( myTextView.draw(canvas) ).

However, the first time I call the PopupWindow the getLineCount() returns the number of characters and not the number of lines (e.g 42 instead of 2) Debug prits suggest this is because the TextView is not properly initiated:

DEBUG/View(207): frame={0, 0, 0, 0} scroll={0, 0} DEBUG/View(207): mMeasureWidth=0 mMeasureHeight=0 DEBUG/View(207): privateFlags={} DEBUG/View(207): frame={0, 0, 0, 0} scroll={0, 0} mText="text to display.."

The rest of the times a pop-up is triggered (by user press on map icons) the view has non-zero parameters, and then line count works fine:

DEBUG/View(207): frame={7, 13, 209, 79} scroll={0, 0} DEBUG/View(207): mMeasureWidth=202 mMeasureHeight=66 DEBUG/View(207): privateFlags={HAS_BOUNDS} DEBUG/View(207): frame={7, 13, 209, 79} scroll={0, 0} mText="text to display.."

As a work-around I tried to draw an invisible PopupWindow before drawing the first pop-up, i.e trying to use it "twice the first time". This didn't help.. I also tried to invalidate the MapView which didn't work either.

Can anyone please suggest how to initiate the views properly so that I can use getLineCount() the way it is supposed to?

Thank you! Vanja

A: 

Hi, as a workaround I would suggest this guy's class:

http://stackoverflow.com/questions/2617266/how-to-adjust-text-font-size-to-fit-textview

I guess it does the job fine, using measureText().

But why this doesn't work - I don't know, have to dig in it quite a bit ;) My suggestion is that at first the text box is really narrow, and lets only one character on one line (this is why it returns 42 instead of 2).

Danail
Hi and thanks for your suggestion! Actually I had an approach like this before. At first it worked for some time, then I got the same kind of issue as above (by some reason). Some View element was not initiated, so measureText() returned 0. Anyway I changed my approach from adjusting the text to fit into one line to adjust the size of the pop-up. With a maximun of 3 lines. //Vanja
Vanja