Android: Can't make small fontsize on programmatic textviews - android

I'm using the following method which programatically creates a linearlayout and populates it with two textviews, adds text and then turns it into a drawable that I later use a layer above a shape. However, I've noticed that I can't create a small fontsize - it seems stuck at a minimum size which is relatively.. large, and anything that I specify below that value just seems to make it look increasingly blurry (but still the same size). What could be the reason for this?
This behavior occurs whether or not I used TypedValue.COMPLEX_UNIT_SP.
Edit: This size stays the same even if I specify something ridiculous like:
.setTextSize(TypedValue.COMPLEX_UNIT_SP, 60);
it doesn't get any bigger - it just gets "sharper".
Edit 2: If I specify the top textview as having a very large size, then the smaller I set the second textview, the smaller it becomes - as a ratio (for example, if I set the top at 100 and the bottom at 50 it looks exactly the same as the top at 10 and the bottom at 5). However, in no way can I reduce the size of the top textview.
Edit 3: If I remove one of the textviews, and leave only the other one as a single textview in the layout - I can't change the size at all. I can only make it more or less blurry depending how low I set the number but it will always appear the exact same size on screen.
private Drawable createTextLayer() {
LinearLayout newLinearLayout = new LinearLayout(getContext());
newLinearLayout.setOrientation(LinearLayout.VERTICAL);
newLinearLayout.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
newLinearLayout.setBackgroundColor(getColor(R.color.somecolor));
newLinearLayout.setLayoutParams(layoutParams);
TextView headlinetv = new TextView(getContext());
TextView bodytv = new TextView(getContext());
headlinetv.setText(headlineText);
headlinetv.setTextSize(7);
headlinetv.setGravity(Gravity.CENTER);
bodytv.setText(bodyText);
bodytv.setTextSize(6);
bodytv.setGravity(Gravity.CENTER);
newLinearLayout.addView(headlinetv);
newLinearLayout.addView(bodytv);
newLinearLayout.setDrawingCacheEnabled(true);
newLinearLayout.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
newLinearLayout.layout(0, 0, newLinearLayout.getMeasuredWidth(), newLinearLayout.getMeasuredHeight());
newLinearLayout.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(newLinearLayout.getDrawingCache());
newLinearLayout.setDrawingCacheEnabled(false);
return new BitmapDrawable(getResources(), b);
}

Change your code to:
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 7);
Change the font size of 7 to any font size as you wish.

This turned out to be quite tricky and took me several hours to solve, but I'm posting the answer for anyone else who ends up in a similar situation.
The cause:
The entire drawable layer was the uppermost layer above a shape, and was therefore stretched to the size of the shape no matter what. In order to stretch it to the size of the shape, the largest view was stretched (essentially, the longest textview) and therefore couldn't be enlarged or reduced in size.
The solution:
I defined two more textviews, gave them the longest string by 1 character, and made the textcolor transparent. I would have preferred to give them blank space as content but android refused to measure a textview containing whitespace, so it had to be actual text. I put one textview at the top and one at the bottom and thus the visible text appears perfectly centered and in the correct font size.
Edit:
There turns out to be a better way of doing this. Define the textview as the same size as the shape on which it sits as a layer and define padding around the textview (play around with it to find the minimal amount of padding needed). If you have several textviews, divide the size of the shape by the number of textviews and give each one a fraction.

Related

90º rotated textview gets size to display text as the horizontal space

I have a textview where I want to show text in vertical (just like rotated 90º). With android:rotation="-90" you can get this.
If the text would have no rotation, and you would like to fit a sentence, you would first look at the width of the textview, since text will try to be in 1 line. Since I rotate the text, here I expect to see the height of the object. However, Android is still considering the width. I need to fix the size of the width, but this causes me a problem for the text: Although the textview has a big vertical dimension and text would fit, Android thinks it needs to use the width to calculate the available space. At the end this causes me overflow of the text, appearing for example, just one word as multiple lines of 1 char.
How can I make android to understand that the space for the lenght to fit the text is now the vertical, instead of the horizontal?

How can I get the size in pixels of the space below the text of a textview? (not the bottom padding)

bottomPadding returns 0 and is set to 0, but my textview still has padding below it's text.
I am trying to draw text on a canvas at the same height as the textview is from its parent but for some reason Paint.drawText() ignores the extra padding at the bottom.
Since the canvas object has its origin a the bottom-left corner, I have to provide the the y position of the text from below.
I can't do the following:
yPosition = textview.bottom + ((rectbounds.getHeight() - textViewHeigth)/2)
This is because the size of the space on top of the text is bigger than the bottom space.
Removing the extra space from the textview would also solve my problem. However, I already tried setting includeFontPadding to false, and setting padding to 0, but neither work.
What happens if you put a 'g', 'p', 'y', or 'j' in your TextView? Each of these characters has a descender, and the metrics for the font will always allocate space for this feature of many scripts. This is to allow for the fact that multiple lines of flowing text ought not be crammed into each other.
If you really need to get the size of a font's descender, you can use a Paint object, load it with a Typeface and ask for its descent(). But a TextView I don't think is going to change the way it renders its lines for you. If you must ignore the descent, draw your own text in a custom view.

Resizing the textview size based on the screen width for all devices

I have problem in resizing the text view .
I have a Layout like this with below components
ImageView| TextView | ImageView
placed in relativelayout(horizontal) and each one width is wrap content and height is fill parent.
PROBLEM
I have a text like this "My name","My name is xyzmnop" now, i have fixed the TextView size to 15sp, so what is happening is in larger devices since Textviews width increases the text "My name is xyzmnop" will fit but in smaller devices since the width is small it will spill out to second line. But i want it to be in same line, as I have the flexibility to decrease the size of the text if the text is lengthy.
please help me with this problem.
i have seen this
Android TextView doesn't resize after changing text size
Android EditText Resize Programatically
Resizing TextView does not shrink its height on Android 3.1
EDIT:
I have also added setSingleLine = "true"; in my xml
Say, tv is your TextView.
Just call tv.setMaxLines(1);
TextView has the setMaxLines (int maxlines) method:
Makes the TextView at most this many lines tall. Setting this value overrides any other (maximum) height setting.
I suppose this might help you if you set the maximum number of lines to 1.
Note: there is also the android:maxlines attribute if you need to set it in your XML layout.
i have solved it using ViewTreeObserver.

How can avoid text from being cut off on a button?

I am customizing a button in Android changing its background (using a 9patch png file) and changing its font typeface. I put the button in a RelativeLayout since I need precise positioning and dimensioning of this item in a custom layout. I am doing all this programmatically (from creation to setting and display).
I found this question with the same problem, but the solution will not work for me. I used
continueBtn.setPadding(0,0,0,0);
but the graphical output remains the same. More than half the text is cut off in the view.
An excerpt of the code I am using is the following:
Button continueBtn = new Button(getContext());
p = new RelativeLayout.LayoutParams(width, 45);
p.addRule(CENTER_HORIZONTAL);
continueBtn.setLayoutParams(p);
continueBtn.setTypeface(...);
continueBtn.setTextSize(14);
outerLayout.addView(continueBtn);
continueBtn.setPadding(0, 0, 0, 0);
No matter where I put the setPadding method call, things won't change.
What am I doing wrong? Any ideas?
You are setting a constant height for the button of 45 (pixels, I guess). However, apparently the text you are trying to put in it is taller than the amount of space android will let you use. Make your button taller (by changing the LayoutParams) and it will fit without cutting off. Alternately, use a smaller text size so the text will be shorter and fit.

Android: Auto-Scale Text in ListView item

I have built a ListView and my items - at least in part - contain titles of various (text) lengths.
In order to enable the user to read as much of the title as possible, I'm trying to change my adapter to auto-pick a feasible font size for my texts.
So I'm working with the TextView's paint object to measure the text in a basline font size (14dp) and try to compare against the available space. If the text is too big, I reduce the font size to 12dp (later I might think about reducing it even further).
// Note: vh.filmTitleTextView is my TextView, filmText contains the title I want to display
filmTitleTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
float textWidth = vh.filmTitleTextView.getPaint().measureText(filmText);
if (textWidth > vh.filmTitleTextView.getWidth())
vh.filmTitleTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
The issue is that on first run, vh.filmTitleTextView.getWidth() always returns zero. I guess this is because the layout has not been rendered before and the size is not yet known.
I can't just go with the full size of the ListView because the textView doesn't have the same width (despite the fact that it is set to layout_width="fill_parent") - there are some elements around it.
Any ideas?
Had a similar problem that was my bane for a long time - this might help ya: Auto Scale TextView Text to Fit within Bounds

Categories

Resources