Putting text into an AlertDialog box as a CharSequence using setMessage() causes the text to display at the default system size.
However, if the text is a TextView object, then it is added using setView() and the resulting output is a tiny font.
I know that I can define a specific font size for a TextView object (that's the point of it after all), but how / where do I tell the project to use the same font size setting as is used by setMessage()? Since setMessage()'s resulting font size depends on the system settings that the user has set up on their device, and I'd like to be able to cater for visually impaired users without the need to resort to a font size menu within my app.
Alternatively, if anyone knows a way of pushing a SpannableString to an AlertDialog using setMessage(), that would be a suitable solution too...
refer to Typography
You can just set the font size to 18sp for Medium size (which is used by setMessage()), and about the user preference, sp itself will scale according to it.
Do this to get the default font size:
float size = new TextView(this).getTextSize();
then set the value to the TextView object you are using.
Related
I'm a novice programmer and I'm working on one of the my first projects on Android Studio for a school work.
I have to create an application that consists on a TextView where the user can write some text and on a button that permits to open a sort of menu where the user can choose the text's size. When the user select an option from this list, the written text should change its size.
The problem is that I don't have idea in which way I can change the size in the TextView when I select an option. Is there a specific function that permit to change the size of the written text?
You could use those
This one is for change font in SP size:
TextViewXXX.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
This one is for make text BOLD
TextViewXXX.setTypeface(null, Typeface.BOLD);
Yes, you can use TextView.setTextSize(float). But, TextViews must have scale-independent pixels to support multiple screens and font size configurations (sp). So, you can use TextView.setTextSize(int unit, float size) where the value for unit would be COMPLEX_UNIT_SP and size is the required size of the text.
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 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.
Is there a method or do you know a possibility to find out the optimal text size in a text view? I think this could be very usefull if you have a changing text in a text view. This could also solve the Problem, that text on a small Screen is to big and the same text size is to small on a big Screen.
A very helpful tool in creating layouts that can fit all screen sizes is the attrs.xml.
Like drawable-hdpi, drawable-mdpi, etc., you can customize the size of texts (and whatever attribute you want to customize for different screens) by creating a size-specific values folder.
Android also comes with built-in "recommended sizes". Try typing in
?android:attr/text
ctrl+space to see the list of recommended sizes for some of the most common elements that has text in it.
For TextViews you can also modify its style (not textStyle) and use some built-in values such as
#android:style/TextAppearance.Medium
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?