How to change theme attribute in code? - android

Is there anyway to change theme attributes dynamically? For example,set the "android:colorPrimary" attribute for a Theme. I know I can apply different Themes to Activity when it creates, but it requires predefined styles in resources, which means the attributes are fixed. If I want to download one Theme from server(it can be a config file with some color attribute defined), this can't work out.

Related

How to change the default theme in android studio?

Every time I go to xml file I've to change my theme first to see how my views will actually look, my parent theme is inheriting from MaterialComponents theme and all of the activities are using the same base theme, but still I don't know why in xml it shows Material.Light as my default theme.
and AppTheme is the theme that I want to set to default, is there any way I can do that.
To set or change the default theme, open AndroidManifest.xml and in the application tag, set if not exist or change
android:theme="#style/Material.light"
to whatever you want. You can change theme of individual activity by adding same line in the Activity tag of any Activity.

How to declare same attributes for two android themes?

I have to declare some base values for my App (like windowNoTitle).In some activities the Theme.AppCompat.Light theme is needed cause of transparent background images. They need a white background but `` In others the Theme.AppCompat theme will be used for styling.
My App uses androidx.appcompat.widget.Toolbar. So I need to extend a Compat theme. I can't apply the default values as parent and apply the style via theme name.
How to apply the default values to both themes but not repeat my self. Is it possible to declare "style variables"?
Thanks in advance

Is it possible to modify a theme at runtime?

I'd like to dynamically change attributes in this theme: Theme.AppCompat.Light.DarkActionBar. In particular, I'd like to change the colorPrimary, colorAccent, and colorPrimaryDark attributes based on the data being displayed so that particular widgets like EditTexts can be themed appropriately. I know that I'd have to change the theme before setContentView(...) of my Activity but how do I change particular attributes in the theme before I do that?
Edit: From #reVerse's comment linking to this post I know that it's impossible to change a Theme as they are immutable. Is it possible to create a Theme programmatically?

How change theme color programmatically: style, attributes android?

I used google but didn't find an accurate solution, for changing themes programmatically.
I want to change everything programmatically: styles, colors, attributes etc., without using style.xml, theme.xml or any other xml file.
I have searched, but styles are defined in an xml file like that Using Themes in Android Applications
I know this is an old question, but if someone's still looking for a solution, checkout the GreenMatter library for the Material theme, or for the Holo theme holoaccent.

Overriding styles in different values files

I'm using the new fontFamily attribute so that I can control the weight of font used. Because this was only introduced in API level 16 I've created a styles file in values-v16. It seems a bit silly to declare the colour, size etc in both values and values-v16 but it is necessary for all styling to be applied. Is there someway to declare font family in the values-v16 styles file and inherit the colour, size etc from the values version of the file.
Use a style inheritance. You can put all of the common (non-API specific) styles into a parent and inherit this in each of the children.
http://developer.android.com/guide/topics/ui/themes.html
<style name="v16" parent="CommonStyle">
Common file:
<style name="CommonStyle" >

Categories

Resources