I am getting this crash in my app Caused by:
java.lang.IllegalArgumentException: Maximum auto-size text size (24.674995px) is less or equal to minimum auto-size text size (31.5px)
With auto TextView. How to handle such scenarios gracefully where this case occurs.
I had the same exact issue come up today. Generally speaking whenever I have an issue I can't explain it's a Samsung device. This is no different just like you said. Basically the default theme on Samsung phones sets a minimum size. For us the solution was to specifically add the autoSizeMinTextSize="12dp".
Here's another solution depending on your needs and exactly why this is failing for you. The problem is that the user has changed the default font scaling size in the display settings of their device (specifically, they have increased the scale to make the text bigger). Thus the size of the text is larger than the maximum you specified, because the default minimum (since you did not specify a minimum, and that default IS scaled by the user preference) is larger than your maximum.
Specifying a minimum is only the solution if you want to ignore the user's scaling preference. In some cases that is required (in my project we simply cannot honor the user's desired text size in a specific case because that text is bound to visual elements with a fixed size).
The "proper" solution, in that it will honor the user's scaling preference, is to use sp units (which are density-independent like dp, but they ARE scaled by user preference) instead of dp units. If you do this you can still only specify a maximum font size, since that maximum will accordingly be scaled along with the default minimum which you did not specify.
So to sum up, you should always use sp units to specify sizes related to text, unless you specifically want to ignore user scaling due to your layout requirements.
Also, this is easy to test. Just go into Settings->Font and screen zoom->Font Size and set it to "Huge". You should already be testing your layouts against these kinds of variables anyway.
You should set the property values of autoSizeMaxTextSize of the TextView to be larger than autoSizeMinTextSize.
Incorrect
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoSizeMaxTextSize="24.674995px"
android:autoSizeMinTextSize="31.5px"/>
Correct
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoSizeMaxTextSize="31.5px"
android:autoSizeMinTextSize="24.674995px"/>
Related
So I have read possibly everything about the difference between sp and dp. I can say that I do understand why sp is better than dp regarding text size. One thing I don't understand and want some help with is how do I make the text look the same with every possible font size the user has selected for their phone? Is it that bad to use dp for text size? (I know that the app won't take the user's phone font size into consideration but at least it will look the same across the board.) Any advice would be appreciated.
To make the text look the same irrespective of the users' choice, use dp. To change the text size according to users' choice use sp. So, if you have a constraint that the text size should remain the same use dp.
Is not bad at all to use DP as text size. It just won't follow the users preferences regarding text size. We have to use the tools we have, to best fit our needs. I always use DP in elements wich i need to maintain the layout. If you don't want the text size to change, use DP. Some times i use textAutosizing When the text changes. And often i use sp, when dealing with "content" text, wich can grow and scroll, shrink and fit.
Ok I know that there are already many questions that ask the same thing as what I am asking, however the problem is that none of those solutions have worked for me or are what I want. My problem is that when I use the setTextSize method of the Paint class, the text appears to be too big on smaller screens, and smaller on bigger screens. What I want is for me to be able to change it to the size I want on my device, and then on any other device the size should change accordingly (scale down, scale up) and it should appear the same way (NOT the same size) on other devices. And I have already tried multiplying the density by the dp I want and all that other stuff but it doesn't work.
First off, any size you use should be based off of sp- scaled pixels. Units in sp scale with respect to the user defined default size. This is to people who are hard of seeing can choose a larger text size, and all text will scale relative to this choice. You should not be designing text in either dp or in px, as neither of those will scale with the system font size. When you call getTextSize/setTextSize it should be treated as sp.
As for it looking the exact same on all screens- you're not going to get that, because different OEMs use different default heights and different default fonts, not to mention the user can override either of those. Pixel perfect should not be your goal. Which is good, because it isn't actually possible.
I am writing an app about eye test. It is necessary to set the standard text size. I used the following code but it showed what I did not expect.
Typeface type=Typeface.createFromAsset(getAssets(),"Optotypes.ttf");
textView2.setTypeface(type);
textView2.setTextSize(TypedValue.COMPLEX_UNIT_MM,25);
textView2.setText(randomLetter);
I expected the textview show a 2.5cm letter but it is not the exact length/height still.
This situation appear also on different device.
The next problem is that the size is different between the original font and ttf I added. (the original font didn't show the text with 2.5cm also.
Is my code wrong or anything else i missed ? Thanks guys . it is important to me.
I think you're missing how Android handles text sizes.
In Android, you should specify text size in SP units, so Android can scale it accordingly to the user's font size preferences. Never specify hardcoded pixels or centimeters.
Check this references for documentation on the subject:
http://developer.android.com/guide/topics/resources/more-resources.html#Dimension
http://developer.android.com/reference/android/widget/TextView.html#attr_android:textSize
What is the difference between "px", "dp", "dip" and "sp" on Android?
If you want to set the text size in SP programatically, you can do this
// same as android:textSize="15sp" in XML
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
-- EDIT
Keep in mind that by just setting a certain text size it doesn't mean that every letter will be of that size. Remember that there are multiple letters with multiple sizes. For instance, with a size of 20mm, this is what you get
Because Android needs to accommodate every possible character in a textview with the size you provided. That being said, textSize is not 100% accurate to what you provide to it.
If this is not enough for you, please provide more details of the problem you have at hands.
I have an Android app that needs some adjustment if the user sets their font size to extra large (via Settings -> Display -> Font size in 4.0 and higher).
Is there a simple way for me to tell what the user's font size preference is
Updated:
in my layout.xml I have lines similar to to setup a button
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:typeface="sans"
Notice that I'm not setting the font size directly. This layout works and looks good in all cases, except for the Extra Large setting. In that case, due to space limitations, it causes the button's text to wrap to 2 lines.
My goal is to make a slight wording change in the case of Extra Large so that it doesn't wrap
There's a FONT_SCALE parameter you should be able to query the system for. I haven't used it myself, but I imagine retrieving its value would look somewhat like this:
float fontScale = Settings.System.getFloat(context.getContentResolver(), Settings.System.FONT_SCALE)
However, I'd also like to point out that usually you shouldn't be dealing with this value directly. In stead, use sp units for textual content so that you don't have to worry about adjusting to user-preferred font sizes yourself, but rather let the system handle that.
Also refer to: Why should we use sp for font sizes in Android?
I'm developing an Android app targeting Android 2.3 (API9).
I want to resize the height of a simple EditText but when I set android:layout_height="10pt" the border of the EditText becomes dislocated.
The problem is shown here
I tried layout_height, height, textsize.. pt, px, sp... but the same problem.
I haven't tried the application on real device yet.
What layout contains the EdiText? You should never use exact pixel sizes when configuring items in Android, since your layout needs to work properly at many different screen sizes and densities. (See supporting multiple screens for details).
If you really want a specific size, us dp for density independent pixels. It should work to set the layout_height="10dp" However, that won't increase the size of the text within the EditText. You need to use textSize for that. You are probably better off setting textSize and setting the layout_height="wrap_content".
See this similar question about the extra line. It looks like setting a background color might fix that.
I have been having the same sort of problem (and it's a real pain). With some experimentation I have found that if you use "dp" and apply this rule of thumb, it should work for EditText fields.
textSize x 3= layout_height
I say rule of thumb because the minimum height before the field "breaks" again is about 29dp. So, for example if you have textSize="10dp", then layout_height="30dp", or textSize="12dp", then layout_height="36dp". You can have smaller textSizes, but it becomes illegible lower than 9dp. I found this works well in relative_layouts with default fonts. Good luck!