TextAppearance property of TextView in Android - android

I am working on android application where I am using TextView to display the text on screens.
I am using this property for the TextView to set the size of the text android:textAppearance="?android:attr/textAppearanceMedium".
Do I need to set the size of that text also or it does automatically manage by Android OS ?
Thanks in advance.

Yes, the resizing is done automatically in the background.
While using attributes like android:textAppearance it directly uses the predefined specific sp values for the textSize as:
android:textAppearance="?android:textAppearanceSmall" => 14sp
android:textAppearance="?android:textAppearanceMedium" => 18sp
android:textAppearance="?android:textAppearanceLarge" => 22sp
Thus it adjusts the values automatically based on the screen density and the user's preference in such a way that it seems equal on all the devices say for example formula for dp values:
px = dp * (dpi/160)
The similar case is for the sp values used only for fonts, it utilizes the same concept only difference being it may vary due to user's fontSize preference as set in settings.
I haven't used that :attr/ type as it may or may not be used and serves the same purpose because the system resource tool knows that an attribute resource is expected in this context, you do not need to explicitly state the type.

Do I need to set the size of that text also or it does automatically manage by Android OS ?
Nope, Using android:textAppearance="?android:attr/textAppearanceMedium will make all of the text in medium size on different devices. So that means your app text on different devices will have the same size and weight.
Setting the size of each text would require calculation to be able to achieve same size and weight.

In Android text elements there is a default which is applied first (similar to webpages). If you wish to override those and define your own, then you must explicitly set them. android:textAppearance is one such override, but there are other attributes you can override individually (as opposed to as an aggregate)... you'll want to see the documentation for that.

Related

What's the different between "android:textSize" and "textSize" in AOSP?

In AOSP Code ,there are many TextView Resource use "textSize" in style.xml to limit the text size, by in a normal alllication ,they use "android:textSize" to limit text size.
So what's the different between those two things?
In style.xml file the textSize is used for mentioning default text size value of that style.
so whenever you call that style in your view you will get that size of text.
Other hand in android:textSize we hard code the text size of that view.
Hope that make sense.

AutoTextView max size issue

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"/>

How to set absolute text size on android (TypedValue.COMPLEX_UNIT_MM)

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.

Determining user font size selection

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?

Autosizing text in TextView on Android

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

Categories

Resources