Is it possible to modify a theme at runtime? - android

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?

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

How to change theme attribute in code?

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.

change style of widgets (editTextPreference, listPreference, dialogPreference) in android

I created an application where I applied theme Theme.Holo.Light. Now I want to change look of my edittexts and dialogs in PreferenceActivity to look like in Theme.Dark. How can I apply different Theme to this particular Widgets?

Android preference title/summary text style

I have a couple custom preference items -- one that displays a swatch of the currently selected color, and another one that displays a thumbnail.
I have a custom layout for these that matches up very well, and have found that I can make the text appearance match by using android:textAppearance="?android:attr/textAppearanceLarge" as part of the TextView's xml. The problem is, while these generally look fine, they must not be the appearance the 'official' preferences use, since the colors end up wrong on some devices. In particular I'm porting my application to the Nook Color, and it uses a light grey background and black text color on the preference screen instead of black background/light grey text. My text color in this situation stays the same, but the rest of my layout is themed appropriately.
I'm really unsure what I'm supposed to do here to make my text match up with the 'official' theme. Should I be using obtainStyledAttributes and running though my layout to set things? The tutorials I've seen on using that so far have been really baffling, and it seems like there must be a textAppearance or style I can set in the XML to fix this.
You've to define your own application theme which inherits from the official theme you want. Actually, you can just define a theme MyTheme extending the Theme.Light for example.
Create an res/values/styles.xml file like that:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="#android:style/Theme.Light">
</style>
</resources>
Then, you just have to apply your theme using the android:theme attribute of the application entity of your AndroidManifest.xml:
<application android:theme="#style/MyTheme">
[...]
</application>

Categories

Resources