I'm trying to make a textview that scales the contents so that it all shows, no cropping, multi lines or elipsis.
I'll quickly run through the layout just to put it into context.
*There's a gridview with several rows and columns. *The items for the gridview are provided by an adapter which returns (pseudo markup :) ):-
<LinearLayout Orientation=vertical>
<MyCustomTextview name='titletext' Not multiline />
<MyCustomTextview name='itemtext' Not multiline />
</LinearLayout>
I've tried overriding onSizeChanged as this is where several google results pointed me and I started with:-
...
if (this.getLinecount() > 1)
{
this.setTextSize(... currentsize -10);
}
...
and this gave me really small text where it would have appeared on more than one line, so I knew I could resize and find out if the TextView had more than one line correctly.
so I progressed to:-
while (this.getLinecount() > 1)
{
this.setTextSize(... currentsize - 1);
}
which I expected to run until the text was small enough, but after the call to setTextSize getLineCount() returns 0, I believe this is because it needs to recalculate the text size so I tried various combinations of refreshLayout(), forceLayout() and invalidate() to no avail.
I've an idea for perhaps a better approach, but I don't know if it's easily attainable:-
onSizeChanged is called after it is put into the Gridview by the Adapter so I will execute my code there, but is the following possible and how:-
- The TextView is set to a width of fill parent, can I accurately tell a pixel size of the width of available space for text? (I believe there may be padding and such applied)
- I then intend to use measureText(String) to see if the text will fit, if not decrease size and check again.
I believe that would be a good solution, but I'm not sure on the accurate measurement of available space, and if the event I've chosen would be best.
Any assistance would be appreciated.
- Anthony