I have a function that generates a random number. Then, I place the number in a TextView. When the number is a single digit, it fills the TextView perfectly, but when the number is two digits or more, it only shows a single digit because the text size is huge.
How do I automatically fit the text size to the TextView? Something like reduce the text size so that all the number will be shown in the TextView.
I don't have enough reputation to comment so I must abuse an answer. No need to reduce the number's size. Just make sure the TextView is set to wrap_context in the layout:width property and also possibly the layout:height property, that should fix your formatting problem.
Related
I need to show the price in the Android app. Design is such that text size of two digits after the decimal point is smaller than the rest of the text. I know I can achieve this with multiple TextView views but I'm wondering if anyone did it without the use of multiple views and how to do it.
TL;DR Price should be formatted like this: 5,oo USD (oo in this example represents two zeros as in zero cents with text size smaller than the number 5 which represents the number of dollars)
You can use AbsoluteSizeSpan for your TextView
SpannableStringBuilder yourText = new SpannableStringBuilder("5.00 USD");
span.setSpan(new AbsoluteSizeSpan(fontsize, true), start, end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
yourTextView.setText(span)
You can find more information from developer webpage
You can
Check the font face that it zeros are smaller that the rest of the digit and apply it.
I've been developing an application similar to the Google Keep, and was wondering how actually they allocate the text size to a note (algo)?
Do they just allocate size depending upon the Note length?
Or they also check for Card Dimensions i-e card width etc?
Its a bit confusing, because sometimes notes have equal card size dimensions with font-difference, and sometimes notes have both different size dimensions and text size?
So basically, the Question is, What is the algorithm/method to calculate the text size for a note?
you have to use auto-fit TextView for this & add it in RecycleView or ListView(whatever you want) item layout. here is one of stack answer of AutoFit-TextView and source code from github android-autofittextview
apply max text size in TextView then it automatically adjust if there is more text
Do they just allocate size depending upon the Note length
No,
Or they also check for Card Dimensions i-e card width etc
No
just make you TexView size wrapcontent its allow you to add all text in textview like whatsup chat thread.
Edited
you have to manage like below way
*in Google keep text size depend of words. text length is 10 then maximum text size. text length is 30 then medium textsize and more then 50 then small text*
they are using two typeface one is for title and another is for text message
I have a TextView that is going to display one single sentence.
This sentence could be shorter or longer, up to 200 characters for example, but could be more.
I am trying to fit the size of the TextView dynamically according to the text that will be written, but nothing that I have found so far works for me.
I have tried the Auto Scale TextView Text to Fit within Bounds but it's not perfect for my needs as it adapts the text to a very small size and writing two lines, leaving a huge blank space on top and under the text.
EDIT: I see that in phones this solution works perfect. However, when I test in a tablet, it does not increase the text size to fill as much as possible. I am not sure, but I think it only decreases to fit. Please, correct me if I am wrong.
Has somebody tried a different solution?
You could try the AutoScale TextView third-party widget.
I have TextView with height and width as fill parent. Is it possible to find out how many characters can this layout hold?
Do you mean how many characters can be entered into the textview and still be fully visible without scrolling? For proportional fonts, that will depend on the specific characters typed, including where the line break opportunities are. I don't think there's a simple way to compute that.
the textView's height and width is fixed.
when the length of string is longer than textview,the last-line text sometimes display half.
I want,when the last-line text can't display complete,discard it.how to realize?
It sounds to me like you already have ellipsize enabled, and that's why you're only getting "half" the last line.
Depending on what you're trying to achieve, you can-
Remove ellipsize settings and suffer
the string cut-off.
Resize your textview so that all the
text will fit.
Change your font size so that all the
text will fit.
StaticLayout has a way of detecting if the last line will cut off (using getEllipsisCount(line)) which is supposedly used internally by TextView, but I've had no luck using that to detect when a TextView is being cut off (see post).
If this doesn't help, please give us a better picture of what you're doing.