multiple drawables from one xml file - android

I want to use a number of ShapeDrawables in my application, which are all similar, but with different colours etc. Obviously I could just define them all in separate xml files, but is there a more efficient way to have one xml definition, instantiate various objects and change the colour either in code or xml? You could perhaps do this by calling mutate() on one ShapeDrawable defined in xml, but this returns a Drawable, rather than a shape drawable.

Maybe use GradientDrawable instead of ShapeDrawable.

Actually it is possible to do what you asked for,
this post shows you how to do it.
I tried it and it works perfectly. I haven't found though how to refer to a single drawable from the list.
It seems Level lists are meant to be used into a single object to represent different states of the object. Alhtough in this case we are using it as a drawable container to avoid having many small files.
Maybe future anrdroid releases will support a dedicated drawable container.

Related

How to set different colors to drawable in a loop [duplicate]

I have a screen where multiple Buttons use the same background Drawable. I have reusable code I use in various projects to add an OnTouch listener that adds a gray color filter while a button is being touched. That usually works fine, but in this case ALL the buttons are tinted when any of them is pressed.
I see an explanation in http://developer.android.com/guide/topics/graphics/2d-graphics.html:
Note: Each unique resource in your project can maintain only one
state, no matter how many different objects you may instantiate for
it. For example, if you instantiate two Drawable objects from the same
image resource, then change a property (such as the alpha) for one of
the Drawables, then it will also affect the other.
The suggested solution is to use a TweenAnimation, which does not seem to work with color filters.
I also saw Android: Cloning a drawable in order to make a StateListDrawable with filters which suggests using drawable.getConstantState().newDrawable(). This does not seem to make a difference. I'm guessing that as long as the same physical image file is used, all Drawables will be affected by a change to any other Drawable using the same resource.
What solution is there, other than creating a second background image to show the pressed state? It would be nice to have a simple programmatic solution I can add to my code and use in every project.
Example that should work for you:
Drawable buttonBackground = context.getResources().getDrawable(R.drawable.bg);
buttonBackground = buttonBackground.mutate();
//Set your filter here

Is there something similar to Android's XML Drawables in iOS?

I'm currently creating a "design language" for my company which includes custom drawables for buttons, sliders, etc. Basically the idea is that I want our apps on Android and iOS to look and feel as similar as possible.
I started on Android and created XML drawables for buttons with simple borders and rounded corners, etc. Because I'm using XML, I can easily change things like the line thickness, radius dimension, and color with a single change. Other files reference things like #dimen/default_thickness, etc.
Is there any kind of similar concept on iOS? What I'd like to do is recreate these UI elements on iOS "dynamically" so that I don't have to create image files for each element. This would make it harder when one app uses one color for objects and another uses something else.
Basically what I want to know is this: Is there any way, with iOS, to create drawable resources dynamically instead of using static, pre-rendered images?
First,
No there is no XML for adding style to buttons or other views.
Second, you can implement the same type of design process by creating a constants file using NSObject, and then just add custom functions, variables for creating buttons... Something like:
+(UIButton *)create_styled_btn(float corner_width, UIColor color) {
....
return btn;
}
And of course you can dynamically change the style of objects through code, just like in android. XML in Android is of course static, so maybe I'm reading your question incorrectly.
All the best.

how to make a DrawableContainer in XML

I need a drawable where contain several other drawables and let me choose one of them to show at runtime, I think a DrawableContainer is a good choice to achieve this, but I don't know how to create a DrawableContainer in XML, I tried to use selector(StateDrawable) witch is a subclass of DrawableContainer, but I couldn't add items with the same properties.
As it's name suggests, a DrawableContainer is nothing beyond a collection of drawables. As to how the drawables work together is defined by subclasses like the StateListDrawable (change drawables based on the state), a LayerListDrawable (draw drawables over each other) etc. So you cant really use a DrawableContainer in itself.
Documentation:
A helper class that contains several Drawables and selects which one to use. You can subclass it to create your own DrawableContainers or directly use one its child classes.

Adding a color filter to a Drawable changes all Buttons using the same Drawable

I have a screen where multiple Buttons use the same background Drawable. I have reusable code I use in various projects to add an OnTouch listener that adds a gray color filter while a button is being touched. That usually works fine, but in this case ALL the buttons are tinted when any of them is pressed.
I see an explanation in http://developer.android.com/guide/topics/graphics/2d-graphics.html:
Note: Each unique resource in your project can maintain only one
state, no matter how many different objects you may instantiate for
it. For example, if you instantiate two Drawable objects from the same
image resource, then change a property (such as the alpha) for one of
the Drawables, then it will also affect the other.
The suggested solution is to use a TweenAnimation, which does not seem to work with color filters.
I also saw Android: Cloning a drawable in order to make a StateListDrawable with filters which suggests using drawable.getConstantState().newDrawable(). This does not seem to make a difference. I'm guessing that as long as the same physical image file is used, all Drawables will be affected by a change to any other Drawable using the same resource.
What solution is there, other than creating a second background image to show the pressed state? It would be nice to have a simple programmatic solution I can add to my code and use in every project.
Example that should work for you:
Drawable buttonBackground = context.getResources().getDrawable(R.drawable.bg);
buttonBackground = buttonBackground.mutate();
//Set your filter here

Programmatically using alternative resource files?

Is there any way to programmatically select an alternative resource file to use in the app? I have a selection of buttons in my app, and want to use a different set whenever my app is in a certain mode.
Is there any way to achieve this other than manually setting the image resource on every image in code?
You can create a layout filled with exactly the views you want and inflate that at any time. Though maybe I'm not understanding your question... do you wish for R.drawable.myImage to point to 2 different things? The answer to that is no, that's not possible, but it seems like it would be pretty easy to get around this need by creating two (or more) "pointer" arrays that can point to whatever resources you want, and set those as the src for your images.
Declare a two-dimensional array to store the resource constant.
int[][] sets { {R.id.a, R.id.b, R.id.c}, {R.id.d, R.id.e, R.id.f}};
You can choose the set of views by changing the first index of the array set.

Categories

Resources