Instead of using this following line for each textview:
android:textSize="40sp"
Am I able to replace 40sp with a variable?
As a side question, I've been recommended to use SP units as it goes by user defined settings. Is this a best practice?
Am I able to replace 40sp with a variable?
You can either use a dimension resource (e.g., android:textSize="#dimen/someSize"), or use a style that does this (per AlexN's answer), or set the size at runtime via setTextSize().
I've been recommended to use SP units as it goes by user defined settings. Is this a best practice?
On newer versions of Android, the user can change the base font size via the Settings application. By using sp for units, your font sizes will scale along with the base font size. If you use other units (e.g., dp), your font size will remain the same regardless of what the user has in Settings. It is impossible to say whether any given use of sp is a "best practice" or not -- I am sure that there are cases where allowing the font size to change would be a bad thing. By default, though, sp is probably the right starting point.
As I know, using SP is a really good practice for Text Size.
And according to your first question - I think we cannot use variable, however, there is a better way. Check it out - http://developer.android.com/guide/topics/ui/themes.html
So if you'll define a style, you can declare your Views in XML like <TextView android:id = "#+id/myId" style = "#style/myStyle"/>
And this style will incapsulate all parameters you want to set to your textViews on that screen.
No, it's not possible to use code based variables in XML files. However, you can use styles for this.
Example:
<style name="MyTvStyle">
<item name="android:textSize">40sp</item>
</style>
Then apply it like this:
<TextView style="#style/MyTvStyle" ... />
A code based approach is also possible. If the TextViews have their android:id attribute defined you can retrieve them in the code with the findViewById method.
Example:
int[] id_array = {
R.id.textview1, R.id.textview2, R.id.textview3 //Put all the id's of your textviews here
}
for(int i : id_array) { //Loop trough all the id's and retrieve the Textview associated with it.
TextView textview = (TextView)findViewById(i);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,40); //Set the text size to 40sp
}
And yes it's always better to use sp instead of normal pixel values. Since sp will scale with device size and user settings.
Related
This question already has answers here:
How to change the Font Size in a whole Application programmatically, Android?
(6 answers)
Closed 5 years ago.
I want to have a setting activity where the user be able to change the font size of the text for the entire app ... I also only have one textview which makes the work easy but I can't figure it out by myself.
for example I have an activity where it displays the texts and it's called (Contentactivity)
and then there will be another activity named (Settingactivity ) where from here the user be able to change the font size of the text of the Contentactivity .
I hope it's clear and thank you very much for your help .
Here is how I did it :
Create a style for each wanted text size :
<style name="RegularSizedText">
<item name="text_size">20sp</item>
</style>
<style name="LargeSizedText">
<item name="text_size">35sp</item>
</style>
Create an enumeration/map of text sizes, associating each style ID with a number (R.style.RegularSizedText <=> 0, R.style.LargeSizedText <=> 1...) - you must not change it afterwards or it will break the settings when updating the app, you can only append parameters but never remove/insert any
In your shared properties (or any other parameters storage system), put the number ID of the style of the current font size - add it to your settings activity
In each Activity of your app, put this piece of code in the onCreate method :
getTheme().applyStyle(myStyle, false);
Where myStyle is the style ID corresponding to the stored parameter (so if the user selected the font parameter #0 it will be R.style.RegularSizedText)
You must never store the style ID directly as it will be broken when updating the app (the Android R IDs are not the same from a build to another).
Just use sp instead of dp when setting your size. This will guarantee that your text always adjust to the screen size and user's preference in Settings.
Check this SO Answer for more detail.
You can change system font using below code
Settings.System.putFloat(getBaseContext().getContentResolver(),
Settings.System.FONT_SCALE, (float) 1.0);
and after this give permission in menifest file
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
In my application I am using a style for textViews with distinct textSize. However based on different device's screen size, I have to change the textSize of all the textView. I can't call all the textViews one by one and change the textSize based on the screen size. So is there any way I can change the style attribute programatically based on the required conditions so that I don't have to set the text size individually?I think people have already asked this questions many times. Still I was unable to find an exact answer for my query.
You could use HTML in your TextViews.
textview.setText(Html.fromHtml("<h2>Title</h2><br><p>This is <b>bold</b> <font size="6">this is smaller</font>,<font size="20">this is bigger</font></p>"));
Output:
TitleThis is bold this is smaller,this is bigger
I want to change the textsize inside a style at run time. Can anyone suggest me how to achieve it. Here is the style "TitleView" in which textsize attribute is defined. I want to change it from 20sp to any other value at run time.
<style name="TitleView">
<item name="android:textSize">20sp</item>
</style>
I want to do it because I've 4 types of textview with different textsize. and In my app user can choose the textsize. So I've changed the text size of other view in relative to the user entered textsize.
Thanks in advance.
You cannot change style attribute at run time.
If you are just trying to change the text size just do:
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
this will change your text size and you can do this at any time you want. It is not necessary to change your style attribute. For setting text size at runtime see this
Edit: The poster has altered the original question. This answer is no longer directly applicable.
This cannot be done. The styles along with all the other resource values are all compiled into R.java at compile time.
Instead you could you create two styles and switch the style at runtime using setTextAppearance
Now, am trying to be a good Android developer and put all my textsize and other size specifications, and all the styles (like, say, button color, etc) in XML files. Am trying to read up on these. In a Stackoverflow thread, I read about dimens.xml. So I created a dimens.xml under each values- directory (like values-ldpi, values-mdpi, blah blah) and put one dimens.xml in each. It works fine. Now, I want to put some style information in the XML, like, a button color. Should I put the attribute in the same file i.e. dimens.xml? I read in one thread that dimens.xml is only for dimensions, and not for style related info. In another thread, I read that the name of the XML doesn't matter. Now, what is a good practice? Keep style.xml and dimens.xml separate ? Or put everything in one xml?
And if, say, for a textview, I want to have both style (like color), and also textsize information in XML, then should I (1) put both color & textSize in styles.xml, or (2) put color in styles.xml and textSize in dimens.xml?
Keep style.xml and dimens.xml separate ?
As soon as style.xml already exists for reasons like these (button colors, text color, themes, etc.) then I believe that you should keep them separate.
And if, say, for a textview, I want to have both style (like color), and also textsize information in XML, then should I (1) put both color & textSize in styles.xml, or (2) put color in styles.xml and textSize in dimens.xml?
I would do the (2). (Although I can't blame anyone who would do the (1).). As far as I know there's no best practice guidance on that. I do it the (2) way basically because I see dimen.xml as a place to host pixel-related values. But this is my preference.
I don't want to specify hard coded test size values in my layout.xml, hence i am using the following specification :
android:layout_height="#integer/intervalViewHt"
and
#integer/intervalViewHt value is as follows:
<integer name="medium">15</integer>
Now, the while inflating android is creating a problem saying that it cannot inflate the view. I want to actually specify value in dp so the actual value should be like
android:layout_height="15dp"
Can anyone help me here ?
What you're looking for are "dimensions" rather than plain integers.
Declaration:
<dimen name="intervalViewHt">15dp</dimen>
Usage:
android:layout_height="#dimen/intervalViewHt"
Have a look at the given link for more examples in the Android docs.
I use something like this for text size across different devices. It's java based, not xml.
tvOutput.setTextSize(TypedValue.COMPLEX_UNIT_DP, getResource().getInteger(R.integer.medium));
I haven't tested the code so you might have to play with it. I'm sure you could do something like.
button1.setHeight(TypedValue.COMPLEX_UNIT_DP, getResource().getInteger(R.integer.medium));
Though again I haven't tried it and you may need something different then TypedValue
What if you give it a String rather than an int. i.e in strings resource you have a string "15dp" that you reference