Change Theme Dynamically from User Input in Android - android

I'm building a material design playground that can switch between the available pre-defined themes that can apply the color, typography and shapes to all the material design components throughout the app.
Right now, the user can choose available themes from the PreferenceScreen:
I got a NEW requirement that should allow the user to enter a value (eg. set color of colorPrimary). From the input, I would like to modify the Theme directly so that it could apply the change in the app.
How can I do this? I'm thinking of giving an option to the user something like below:
Here's a link to my sample material design playground app:
https://github.com/ciscosoriano/material-design-dynamic-theming

Seems like you need something like this android-how-to-change-application-theme-programmatically
link: android-how-to-change-application-theme-programmatically
This should be easy and there are lot of tutorials out there, don't forget to checkout
Basically what you need to do is:
apply statically defined theme(in res/values) as usual before activity is launched.
Now get the user input when activity is launched
Then get the current theme attributes using appropriate api and recolourize the ui controls dynamically in the program.
restart activity with the updated theme.
Some more link: Set theme color dynamically
You should be comfortable handling the activity lifecycle to view the updated theme when set.

Related

Android How to set windowActionModeOverlay using code at runtime

I have a android custom view that start a custom action view when long clicked, as such, it require the theme of the activity to have the style "windowActionModeOverlay" set to true. I don't want to define a custom theme. I want to use whatever theme the user provided for the activity, and set the style to true at runtime using code myself.
Is this possible ?
Please note any suggestion by changing style xml file is not what I am looking for, unless it allow user to use whatever theme they choose for their activity.
Thanks
There are tons of solution for changing the style of a single view. There are tons of solution for changing theme at runtime, but I only want to change a single style of a theme, and so far, I can not find a solution myself or online

Create Style Programmatically

I have a requirement to display a theme that comes from a webservice (return the color etc). What I want to know is can I create a style programmatically? I have my code setup to incorporate theme and I have 2 themes defined in xml which work perfectly fine. And now I want to add a third theme which is not locally in an xml. Is this even possible?
The other approach to achieve this is to create custom classes for views and apply the style according to the theme. Which is I don't want to do, as it is very tedious and error prone.
Any other suggestions are welcomed as well.

Change App Color

Is there a way to replicate the color changing ability provided by QKSMS .
It allows a user to select a color and then changes the app color and everything to that color.This is all done dynamically the moment the user chooses a specific color.
You can try this cool online tool: http://android-holo-colors.com
This enables you to create many themes for app widgets with your preferred color. You can then change your app theme by saving values for your theme in SharesPreferences or something similar and apply them in onCreate of each activity you have in your app. You can also create the themes easily in styles.xml. For each view, it has its own style with its own attributes so you will need to learn how to edit the theme of each view you want by searching...
Hope that helps.
Some Examples:
ActionBar:
https://developer.android.com/training/basics/actionbar/styling.html
Buttons:
https://www.androidcookbook.com/Recipe.seam?recipeId=3307
SeekBar:
Android - styling seek bar
Switch:
How can I style an Android Switch?
And a lot more... You will have to search for what you want.

Changing app theme at runtime using using external theme file

I want to produce a single app that would let the user select a theme and have this applied over the entire app.
I understand that this can be achieved by using setTheme in onCreate of each activity.
But I need this to work in a different way. I don't want to have the themes stored in theme.xml or styles.xml.
I want to have a list of themes stored on the web and be able to dynamically download a new theme and have it applied in the app. I want to be able to create new themes without having to build a new version or an updated version of the app.
Images would be easy to replace. Just download from a url and store locally to be re-used. But the actual theme of the app, the colours of buttons etc should be changed at run time from a theme.xml file which isn't part of the apk but is fetched online.
Is this possible?
It depends how much styling you want to be able to do. You currently can't set view items styles grammatically outside of using a resource. But you can control things like text color and background color. If that's all you need to change, I would recommend writing a Theme factory class for you app that you use to get each view element you need. For example a getButton() function that will return you a button with the background color and text color you need.

Consistent UI color in all Android devices

I noticed the UI color (eg Button background/text color) all changes from device to device, based on the current theme that is being used in a device.
What is the best practice to apply custom UI colors for Android app, so that I have same color scheme for my app in all Android devices. I can set text/background color on a UI item. I'm wondering if there is a single place where I can define all the colors which will override the current theme applied on the phone.
thx.
Yes, there is a single place where you can define these values for your app. See Styles and Themes in the Android docs for how it works.
A style is just a mapping of values to predefined names. If you find yourself repeating a number of common attributes in your layouts, you can factor that out into a style. For example, you might have a special button style that defines a specific background and text color.
A theme is a sort of meta-style. It can be applied to an Activity or even a whole application through your AndroidManifest.xml. Among other things it defines the default styles for widgets and values that control other parts of the look and feel for your UI.
When you're trying to blend in with the system in an otherwise custom UI for your app, you can query the current theme for values. Just like you use the # reference syntax #android:drawable/foo when referring to a system resource, you can use the syntax ?android:attr/foo when you want to use the value stored in the system theme attribute foo.
In your case, if you want to change the primary text color across your app, apply a custom theme that sets the attribute textColorPrimary. If you just want to be sure that an element of your app is using the primary text color as defined by the device your app is running on, you can set android:textColor="?android:attr/textColorPrimary". The same principles apply elsewhere as well.
If you want to see what attributes are used in the system, they are defined as part of the Android framework in this file: frameworks/base/core/res/res/values/attrs.xml. Look at the children of the XML element <declare-styleable name="Theme"> at the top. To see examples of what the system sets these to, see themes.xml in the same directory. Finally, not all of these attributes are public - non-public attributes cannot be set by an app, they're implementation details of the Android framework. See public.xml for the complete list of which attributes are available for use in apps.
Best practice is to apply a custom theme to your application, and override as much of the default properties as you need.
Almost everything can be changed, except
The Menu
Some properties of AlertDialog (these can be changed using a custom dialog)
OS provided views such as the Quick Search Bar (QSB)
If you like the look of the default SDK resources then you can find these in sdk_folder/platforms/android-9/data/res/ (replace 9 with the SDK version you want the resources from) - copy the ones you want into your App and reference those.
You can take a look at the theme the SDK uses:
themes.xml
styles.xml

Categories

Resources