Text and fuck no cc needed at all
See my (presently un-answered) post about that here. The difference between mine and Chase's soluton: resizing is done even if the user DELETE some chars. I've tried 6 other answers, but this one gets me the best results.
I'm not familiar with nine-patch, it appears to be a image format thing though... Text View; public class Auto Resize Text View extends Text View Wonder why this hasn't been 1'd more.
I'm looking for documentation on how that could be used with a textview. Reading up on it a little further I'm getting the impression that nine-patch is just for resizing something that already has a defined format- but I need something that will take a string and find its optimum size and format within certain bounds. It's certainly a hack by nature but a very simple solution. So I changed to 'measure(width Measure Spec, height Measure Spec);" and it works.
A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. For an app that's only laying out a few, non-scrolling Simple Line Text Views this is the easiest way to go. I just tested on Android 4.4(Nexus 5) and Android 4.0.4(Samsung SII-LTE) and it worked fine with both of them (I even used custom OTF Japanese font).
From there, I step down by 2 font pixels and remeasure until I have a size that fits. The error you got was probably from when there was not enough height to render even one line of text. (I work in Shibuya so I'd be up for a beer anytime :) UPDATE: Following code also fulfills the requirement of an ideal Auto Scale Text View as described here : Auto-fit Text View for Android and is marked as winner. This change makes it resize until the text fits both vertically and horizontally. :)As mentioned above, Width() just returns the width it was created with. Text View; /** * Text view that auto adjusts text size to fit within the view.
At the end, if the text still does not fit, I append an ellipsis. UPDATE 2: Support of maxlines added, now works fine before API level 16. In // Get the required text height int text Height = get Text Height(text, text Paint, width, target Text Size); // Until we either fit within our text view or we had reached our min text size, incrementally try smaller sizes while(text Height // Get the required text height int text Height = get Text Height(text, text Paint, width, target Text Size); int text Width = get Text Width(text, text Paint, width, target Text Size); // Until we either fit within our text view or we had reached our min text size, incrementally try smaller sizes while(((text Height // Set the text size of the text paint object and use a static layout to render text off screen before measuring private int get Text Width(Char Sequence source, Text Paint paint, int width, float text Size) Change 2: Fix a Empty Stack Exception in the Eclipse Android Layout Editor Under rather obscure and very precise conditions, the Layout Editor will fail to display the graphical display of the layout; it will throw an "Empty Stack Exception: null" exception in com. What I had to do to make this work was to create the Layout with a width of 4096, and then call get Line Width() on all the lines and return the max value. * If the text size equals the minimum text size and still does not * fit, append with an ellipsis. Text View; /* Based on * from */ public class Font Fit Text View extends Text View based on Kirill Grouchnikov's blog post about custom components used in the new android market app. On the other hand, mosabua read my post and told me he was going to open source his implementation which was faster than mine. Text Thanks to Chase and onoelle, for the lazy programmers, let me post here a working version of their fantastic merged code, adapted on a Button, instead of a Text View. * * Everyone is permitted to copy and distribute verbatim or modified * copies of this license document, and changing it is allowed as long * as the name is changed.