Set style from xml to elements inside a ViewPager - android

I like styling my android views from external styles.xml files. I was wondering what approach can I take to apply externalized styles to views that are created programatically and not in XML layout files, for example, views to be inserted on a ViewPager.
Another use case for this would be styling from XML files custom components that don't have a corresponding XML layout.
Any help would be appreciated :)

Ideally you'd be able to use the View's constructor that accepts the int defStyle attribute. While I haven't used this constructor there may be an issue with styles not being applied. It sounds like standard views may not handle the style in an expected way. Unfortunately, it sounds like your best bet might be subclass the view and override the constructor to ensure that your style is correctly applied. I'd also take a look at the source code of view to see how styles are handled in View.java's constructor. Hope this helps.

Related

Is there a way to programatically create an imageview object based on xml definitions?

Essentially I want the src, tint, layout:width, layout:height, alpha etc. to be defined in an XML file, and then do a "new ImageView()" based on the XML.
The simplest solution is to create a layout resource that describes your ImageView, then inflate it with a LayoutInflater. In general, try to use a LayoutInflater that you get from an activity or fragment via its getLayoutInflater() method, so that styles and themes get applied the way you might expect.
Nothing else built into Android offers what you want, though one could certainly implement their own "inflater of layouts" that read in XML and created corresponding widgets.

Override a style in Android without modifying layout file

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 can I programmatically change the value of a color in colors.xml?

I am developing an application that will be themed with different colors and images for different clients. While I have the option to re-write the colors.xml file with the custom colors at build time, I am leaning towards setting up the colors at runtime. What I am wondering is if that is some way to programmatically change the value of a color defined in the colors.xml file and have that new value take effect in ALL places where it is used in the layout definition.
So in other words if I have:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<color headerColor="white">#FFF</color>
<color backgroundColor="black">#000</color>
</resources>
And a layout file with something like:
<TextView
android:id="#+id/listItemHeaderActivity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#color/headerColor"
android:background="#color/backgroundColor"
android:text="#string/listTextHeaderActivity"/>
Can I change the value of headercolor and backgroundColor in Java and have it take place in all Views that use those values? Or will I have to change each relevant View color individually whenever I display those views?
Thanks in advance.
It would help if you described why and how you are using this dynamic color, and why you want to do this at runtime instead of build time. But assuming it has to be the way you asked...
I suggest writing a helper function that all places can call and set them.
public int getMyColor(...) {
// figure out which color to use, via a database call,
// an asset load, some algorithm, or whatever you need
...
// once color chosen, create an RGBA integer for it
final int myColor = ...
return myColor;
}
Now call this on every activity/fragment that needs it and set the color attribute(s) on the appropriate views as needed. (With View.setBackgroundColor(...), etc.)
However, to allow this to work in XML settings and/or development layout previews, you would have to write a custom view class to call that helper function too. Depending on where and how you will be using this color, it may not be worth it.
This solution isn't very elegant and requires a lot of calling this custom getMyColor helper function in every activity/fragment that needs it. If it is only set one or two places though, it's probably not a big deal. Again, knowing why you want to do this instead of asking us how to do it, may yield a better alternative for you.
For example—this isn't an answer to your question—but have you thought about themes? It will still have the problem of having to set them at build time unless you want all of the above, but depending on how you're using this color, it might be better than the mess I outlined above.
I ended up doing something somewhat similar to what I think Jon may be describing. I created a class extending Fragment (or SherlockFragment in my case). That class has a new method called "setCustomColors" that just takes the fragment view and searches all of its children for certain types of views that need to be customized. I then extended that class for all of my Fragments, and in the onCreateView method, I call the function, passing in the current fragment view. Let me know if that explanation is unclear (although maybe this isn't a problem very many people will have).

Is it possible to inherit the attributes of a xml file?

For example, when creating list items, I would like to reuse Android's simple_list_item1.xml or simple_list_item2.xml, but I would also like to tweak them.
So, is there any method to inherit the attributes of these views that are declared in xml?
My solution for now is to copy all the attributes in simple_list_item1.xml to a self-defined style, then have the TextView inherit the style. But I hope to get the data directly from android so that if things changes in the future, my views also inherits the changes.
Its not possible to use inheritance in a OOP model. The best way to use this is to copy and paste the source into your own project.

Preferred method for XML layouts: explicit attributes or styles.xml?

Currently I'm mixing explicit attributes (layout_width, height, alignment) on my various XML layouts with coded styles in styles.xml, plus outsourcing colors from colors.xml.
From your experience, what's the recommended way to organize an android app's layouts?
Usings styles is way more organized and makes the xml code easier to read, I think that the visual editor for layouts sucks, so the best way to edit is still to manual edit text the file. Easier reading is a big plus.
I use style anywhere im repeating layouts, something that is very useful is that any explicits attributes will override those of the style, so you can use styles and if what you need is almost the same, you just explicitly redifine the attribute.
Any time you need to fix a layout issue you can update the style and not need to update it a million times.
On the other hand if you are just using that layout "style" once, there is no reason to write an actual style for it, just do it all explicitly.

Categories

Resources