As an Android developer new to iOS/swift, I am trying to implement a global theme/style which every view controller can inherit. I expect one file which I can modify which would affect all View Controllers's UI. This is possible in android. Does iOS have anything similar?
The best match for what you are looking for is probably UIAppearance, although not quite the same.
What it allows you to do is basically setup each component once, and then have that be the default look for all other components.
An example for setting button colors (you could do this in your AppDelegate):
UIButton.appearance().setTitleColor(.white, forState: .normal)
UIButton.appearance().backgroundColor = .blue
Try looking at these tutorials:
https://www.raywenderlich.com/108766/uiappearance-tutorial
http://blog.iseinc.biz/use-uiappearance-create-ios-app-themes
Usually if you want to do something like that, you can go to the AppDelegate File, and add code like the ones below:
[[UITextField appearance] setTintColor:[UIColor redColor]];
[[UINavigationBar appearance] setTranslucent:NO];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
For example textfield by default has a bluish tint color, but with the code above, the default tint color becomes red. The single line of code you place will affect all UI related to it. Try it out.
Related
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.
I have colors of text and button come dynamic form backend,
I need to change this color of text dynamically when change happens in backend all at time.
I could make it manual
textView.setTextColor(getResources().getColor(R.color.text_color));
Is there a way to change group of text color dynamically or must set every text color manually in code?
I searched for how to change theme colors dynamically at run time and i found this answer this answer.
But I also search again and I found this github
but it doesn't work on Android Marshmallow (6.0+) and it's use is discouraged! as he say.
Is there any lib or method to change the theme on runtime?
So far it's impossible and not viable because of themes are immutable.
GreenMatter becomes outdated so regretfully the answer of your question is No way.
More precisely, the color overriding at runtime is not working. There is no fix found at the moment. The future of this feature is uncertain.
I want to know how all the Android components such as dialogs, buttons,.. will look like on different themes.
Somebody got a blog or an article for this? Thank you.
Here is an example of different Android themes:
http://android.appstorm.net/how-to/customization/dress-up-your-android-with-gorgeous-themes/
As a developper, I'll say that the appearance depends on what you're using for example for a button you may re-program your own button to use in your custom theme by adding an image in the button's background, change the button's shape or color .. and of course you can also change the look at each state (pressed, focused,etc.).
I'm looking at how to give an app that I develop and deploy it's own look and feel. So I read about the Style and Themes on developer.android.com. After some initial success with text color, text size, background color... I seem to get stuck at changing buttons, toggle buttons... It appears to me that to change the color of a button a .9.png file must be created (possibly for the different dpi's). Looking at the artwork in the default style, I see a large number of these .9.png files. To have a consistent style they should all be updated.
Is it correct to say that defining a new style involves modifying/recreating the .9.png files?
If no, how should one go about modifying the style of these .9.png based elements?
If so, are there any tools that assist with creating a custom style? And are there any style packages that can be downloaded/purchased?
I'm not sure it's a good idea to give a new look to every UI control in your application unless you are a very experienced designer. Probably, we can't beat Google designers at their craft and it would be better to comply with existing styles adding some cool features instead of changing button colors at random.
How would you implement different color themes in your app?
All I can see now is plain set color onCreate every activity and control...
Also, how would you store different color schemes in xml?
Just an entries of with different names?
Thank you!
Use custom Themes, which are declared in XML. They are very similar to CSS, if you've used them before.
EDIT:
Here's a better example of changing the theme at run time