I am new to Android app. development. I need to change the existing style at runtime or can create new styles at runtime and I need to apply it for my buttons and textviews..
Can any one suggest me?
There are several methods to do so (most of them are provided by the super class TextView):
setTextColor to change the color of foreground
setTypeface to change the font style
setBackgroundResource(int) to change its backgrond
setPadding to add padding
All of them work for EditText, Button and others, since most of them extends the TextView class. So, my suggestion is: RTFM... just kidding... read the documentation to see the available functions.
Yesterday I was trying to do something similar.
This links gave me enough info:
Building Custom Components
Custom Android Button Style and Theme
Understanding Android Themes And Styles
Related
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.
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.
My experience up until now when dealing with styles has been to create a style.xml file and create the properties I want for the style. If I want my style to be based on an existing style, I use the parent attribute. I then specify the style inside of my layout file on the controls that I want to apply the style to.
Where I am at a loss is when I want to use system styles and only update certain properties. I am wondering whether I can leave the layout files alone and not bother applying any styles to the controls. Instead, I would somehow update the property of the system style and that would update everywhere in my app where that style is already being used by default.
More specifically, I want to change the background color of the Actionbar but haven't found a way of doing it other than the way I described above.
You're probably looking for themes, which are collections of styles, applied either globally throughout the application, or for each Activity in particular. Start with this document and investigate further.
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
I am following an example project "Sky" by Jeff Sharkey using styles in my layout. It is working out quite well, however, I cannot determine how to specify the font type-face using the styles. Without this, I will have to apply the font explicitly to every TextView, EditView, etc., whereas I want to control where and when it is applied throughout my application.
It seems that it is not possible from xml/styles... You can only specify it from java code.
check here if you are interesting.
You can do this by creating your own textview class and use that in your layouts.
Inside your new custom class, set the font you want.
Et voila!