I have made an component for Android, which uses two drawables.
Using static values for the drawables in the code, the component works but now I want to declare the values in the properties (XML) does anyone know how to do this?
*Edit;
Is there a way to do this without using the attrs.xml?
You will need to set up a res/values/attrs.xml file to declare the attributes, then go through some code to retrieve those values inside of your View's constructor. I have a sample here that demonstrates the technique.
Related
Problem :
I am trying to change the look and feel of my Android app, on the fly. Something like, the app starts up and gets a set of values from the server. These values are the elements that typically go inside colors.xml. What I am looking for, is a way to dynamically change the elements inside the colors.xml and update it with these new values received from the server. My understanding is that normally, this cannot be done directly. But has anyone found a workaround?
What I want to avoid if possible :
I would like to avoid setting color values inside each activity's onCreate() method for each element in that view. If at all possible, I would like to avoid this.
Any thoughts?
You can achive this change by newly introduce firebase remote config which provide remote config to change theme color or any other values necessary for app like promotion,updates etc
You can refer this Example
Unfortunately all color values (and other resources) inside the resources directory are hardcoded as static final ints. This means there is no way to change the values at runtime. You can however use one of the previously suggested solutions or have a look at this excellent explanation: https://stackoverflow.com/a/33992017/3662251
For a nice workaround that overrides the activity's getResources method and implements a custom Resources class which is in my opinion the most seamless solution: https://stackoverflow.com/a/34178187/3662251
I have did that in my app getting Hex color code like #06FF67 from my server and stored in sharedpreferences - https://stackoverflow.com/a/23024962/4741746
And when need to set new value that coming from server just override same shared preferences value with new data and set to app
Or you can use Random Color genrater also -https://stackoverflow.com/a/5280929/4741746
How can I generate a live resource class just like what android generates based on its layout xmls, R.java, based on my customized xml file?
Is there any specific plugin for android studio to actively watch for a particular xml file, namely mylayout.xml, and generate a java class, namely myR.java as I'm writing codes?
EDIT: What I want to do is to create NON-VIEW objects from an xml file which defines objects and their attributes, but not in run-time. My intention is to auto-generate a before-run-time class which contains those objects ids and there would be such methods like getMyObject(int id) which automatically instantiate the object from its relevant class.
Android will automatically add the things from any custom XML layout to the R class. You do not need to do anything special for this, beyond coding your custom layout in line with Android's style and requirements. See Creating Custom Views.
http://androiddrawableexplorer.appspot.com/
I want to be able to use these drawables inside my projects, but my projects are generated at runtime. I only use XML to define new activities, because quite frankly, I hate working with XML from within java. I'm looking for a mathod similar to this. One that doesn't use any XML to 'findByID' the component it needs. Thank you.
Drawable foo = new Drawable(bar);
You can use this :
Drawable d = getResources().getDrawable(id)
So what exactly is it that you are looking for? You want to build activities at runtime without XML? Do you want to just be able to manipulate the android drawables?
Well this is how you use the android drawables:
How to use default Android drawables
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.
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!