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.
Related
I'm writing my first Android-App. The App should solve Sudokus for the user.
So the interface should look like the typical Sudoku-Layout with some Buttons down below. For the number-fields i just used EditTexts.
At first i used a ContrainsLayout. That worked fine so far but the problem is that the editTexts on the right are not visible anymore if the screen of the user is too small.
I use a fixed height and width of 40dp for the editTexts and a constraint of 1dp between the editTexts.
But i cant get it to work that way on different screen sizes.
So i used the GridLayout. There problem here is that i cant get the whole thing to be in the center. And besides that i dont know if its possible to stretch a TEXTview over multiple rows?! I would need this to show the errorText in case the sudoku isnt solvable.
I hope someone can help me out or at least give me some ideas :)
Thanks in advance
If you want the same result for different screen sizes the recommendation is constraint layout but the reason you don't have the same result is that you used hard coded width and height and on different sizes you need to change the size that you hard coded.
The best thing to do is to make separate xml files for screen sizes
for more info you can see here: how to have different sizes supported
Hi guys I've wondered if I could resize the TextView's text in order to make it fit withing the text view.
I want the text view to have a default text size, but in case the text is too long and requires more text than the TextView could offer, the text size should get smaller.
I've seen this answer which is awesome, but it's also from 2013, so I've wondered if there's a simpler way of doing this. Android keeps changing and evolving so I just wanted to make sure there's no easier solution first.
You can use AutoFitTextView for better result .
A TextView that automatically fit its font and line count based on its
available size and content.
There is no built-in way of achieving this in Android, so you'll have to resort to 3rd party classes and libraries to fix your problem (Like the one you linked)
I have tried a few different classes i found from googling around about 6 months ago, but i never found a 100% working solution, there were always some little kinks or flaws in my experience.
I got stuck with these problems :
I have a button . I want to make it (programatically) as small as I want ( e.g. width = 1, height = 1). I tried with setWidth(1) and setHeight(1) but it doesn't work . I searched and I found something about small buttons. So i created my button like:
Button b1 = new Button(mContext,null,android.R.attr.buttonStyleSmall);
b1.setMinimumHeight(0);
b1.setMinimumWidth(0);
b1.setWidth(1);
b1.setHeight(1);
The buttons got smaller , but not small enough . They still seem to have a min size bigger than (0,0) . I could set at every size i wanted by using params, but I don't understand why it's not working without using params.Please give me a solution or just explain me why it's only working this way.
I need to make the text inside a button to fit the space. (i want the text to be as bigger as possible). I had to do a similar job for a textView . The solution I found was to do a binary search for the size of the font (initially i set the maxWidth, textView.setMaxWidth(maxWidth)) and then use textView.measure(MeasureSpec.Unspecified,MeasureSpec.Unspecified) and use getHeightMeasurement to check if it fits the maxHeight. This solution worked for the TextView, but not for the button.
That is because whatever i do , the button.getHeightMeasurement() returns a bigger value than what i wanted ( this happens even when the text inside the button is "" and the font size is 0). So please help me. Ask me for pieces of code or clarification . It's very annoying.
Thanks and sorry for my bad english.
Is it possible to set TextView's text size to be the same as TextView height, when TextView height isn't predefined(WRAP_CONTENT or FILL_PARENT)?
solution : Auto Scale TextView Text to Fit within Bounds
i also wanted to do something like this and the closest you can seem to get is to say android:textSize=20dp (or whatever size you think is appropriate) for either your style or each element that is displaying text. since dp is device independent pixels, if it appears to be taking up the whole screen on your device, then it is supposed to appear that way on all other devices too. you might want to check on this as you might have to choose a different dp value for each of the different size/density combinations possible (depending on what kind of devices you are aimed at, also whether you are allowing the use to change the orientation) this has all that info.
I've found the library that do exactly what I want : SizeAdjustingTextView
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.