Change the attributes of a style dynamically in android - android

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

Related

How to create Font size options (Small, Normal, Large ) for the entire app? [duplicate]

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

How to know the font family of default style in notification?

I create my custom view for notification and use TextView instead of Action.
The problem is: I want the TextView to be displayed with default style (as in default notification), but the TextView is displayed in different style.
So I need to know exactly the font-size, font-family of the default style. If I know these values, I can set them as the style of TextView.
Can anybody help me?
Use android:paddingLeft="8dp" for the text SHARE and decrease textSize by 1sp or 2sp.
Basically, the font size used are as follows :
micro : 12sp
small : 14sp
medium : 18sp
large : 22sp

Is there any way to change a style attribute at run time in android

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

TextView with custom font isn't centered vertically

I need to use a custom font (VAG Rounded, probably not relevant) but the font changes how my TextViews react.
In the image below, you can see the two textviews with a black background. The left one use the custom font, the right one the default system font (Roboto ?). Both of them have the same xml properties and size, but the padding is not the same and more important, the left one isn't centered vartically !
How can I make the TextView draw its content well centered ?
You can try to remove your customs font padding from your text style (styles.xml):
<item name="android:includeFontPadding">false</item>
If this still doesn't work, i would set a general padding in your styles xml.
I think that you have to set android:layout_height="match_parent" and then also android:gravity="center"
then if you post also your source code we can give you more information
I could not change the font, and I wasn't going to edit each character using an editor (I don't even know what I should have done to fix it).
Si I ended up measure the difference with the default font and I added 0.15f * fontSize in the padding top...

android making superscript text in textview got cutted

i have a layout containing multiple TextViews containing text with superscript... to make a portion superscript in text i have used <sup>superscript text</sup> tag in string.xml within text the problem here is my text got cutted in emulator view i have also tried to add padding but output is same nthing is working for it like , changing margin,padding,font size...please help here below is image what i get on emulator........
Make it like this.
< sup>< small>superscript text< /small>< /sup>
A further problem remains: the superscripted text is not properly scaled, i.e. font size reduced.
So padding is just a weak solution to a bug in the proper rendering of a [super|sub]script.
I am investigating a fix for this...will see.
if you want small superscript text than you can use
<sup>< small>text< /small>< /sup>
if you want to more small than you can use <small> tag twice like below
<sup><small><small>text</small></small>< /sup>

Categories

Resources