In my case, they are cat1. Add an xml file in the same directory, call it transition. Here we will add a transition node with 2 item nodes inside, each representing an image:. Add an ImageView , set its source to the first image:. When that's done, go to MainActivity. Set trans value to the transition resource we created in the drawable directory:.
Now add a click listener to the ImageView object. In the event of a click, use the setImageDrawable method to apply the transition resource to the image. Then we call the reverseTransition method of the transition, passing as the parameter, which is the effect time.
Instead of reverseTransition , you can call startTransition , which always starts with the first layer fading out and the second layer fading it. The reverseTransition method handles the transition both ways. Using level list drawable , drawable attributes of a view can be set to deferent drawables at run time by calling setLevel on Drawable object and passing level value to it. The level value points to a drawable in the level list drawable. You can define level list drawable xml with different drawable items setting max and min values for each drawable.
At run time, depending on the value passed to setLevel method, a drawable from the level drawable list will be picked and displayed. Below example defines level list xml with three dawable items with different levels and level ranges. On clicking a button, level is incremented by calling setLevel on Drawable that will make it show corresponding drawable from the level list. Every time the button is clicked, image will be changed to reflect level value.
Using scale drawable , you can specify how a drawable can be scaled depending on the level by setting scale height, scale width and scale gravity. Below is an example scale drawable which is displayed in image view. As the level increases, the drawable that is displayed in ImageView will scale. Hire me About Read Contacts Search. Bitmap File. A bitmap graphic file. Creates a BitmapDrawable. Nine-Patch File. A PNG file with stretchable regions to allow image resizing based on content. Creates a NinePatchDrawable. Layer List. A Drawable that manages an array of other Drawables.
These are drawn in array order, so the element with the largest index is be drawn on top. Creates a LayerDrawable. State List. An XML file that references different bitmap graphics for different states for example, to use a different image when a button is pressed. Creates a StateListDrawable.
Level List. An XML file that defines a drawable that manages a number of alternate Drawables, each assigned a maximum numerical value.
Creates a LevelListDrawable. Transition Drawable.
An XML file that defines a drawable that can cross-fade between two drawable resources. Creates a TransitionDrawable. Inset Drawable. An XML file that defines a drawable that insets another drawable by a specified distance. This is useful when a View needs a background drawble that is smaller than the View 's actual bounds. Clip Drawable. An XML file that defines a drawable that clips another Drawable based on this Drawable's current level value. Creates a ClipDrawable. Scale Drawable. An XML file that defines a drawable that changes the size of another Drawable based on its current level value.
Creates a ScaleDrawable. Shape Drawable. An XML file that defines a geometric shape, including colors and gradients. Creates a ShapeDrawable. There are five drawables that are most important to know: Shape Drawables. The biggest one seems to be the idea that you can use a shape as a text color and that doesn't seem to make sense.
java - Using shape drawable as my background xml - Stack Overflow
You can use a color as a background of a shape, and you can set a shape as the background of view but you can't set a shape as text background or foreground. The other thing that looks wrong is that in the alternative XML files that you tried, the end quotes around the color values are missing, so that shouldn't compile at all. You are giving a wrong hexa color attribue.
Please don't start your titles with the words: You have wrong color settings, you must specify 4 byte colors, eg: Aditya Vyas-Lakhan 9, 11 44 Lucas S. It's unclear how 9 people rank that answer as the solution to the stated problem! See developer. Furthermore I've published apps that use bit color shape drawables. This is typically used for customizing the view graphics that are displayed within a particular view or context. For a list of the default drawables for every version of Android, see the androiddrawables site as an excellent reference.
Drawables can be an initially overwhelming topic because there are many drawable types used in different situations such as drawing shapes, setting state behaviors for buttons, creating stretchable button backgrounds and creating compound drawable layers. There are at least 17 types of drawables but there are five that are most important to understand:. This is used to create a complex shape that can then be attached as the background of a layout or a view on screen. For example, you can use a shape drawable to change the shape, border, and gradient of a button background.
A shape is simply a collection of properties that are combined to describe a background. The shape can be described with properties such as corners for rounding, gradient for backgrounds, padding for spacing, solid for background colors, and stroke for border. Note that drawables can be applied to any view and are usually set with the background property referencing the drawable resource.
Shapes also support gradients backgrounds as well as supporting properties such as startColor , centerColor , endColor , angle. Different gradients such as radial, linear or sweep can be selected using the type property.
Android Shape Drawables Tutorial
Using solid color shapes and gradients we can customize the appearance of buttons, layouts and other views without requiring the use of any images. Note that custom shapes can be created at runtime using other shape drawable types using PathShape and ArcShape. A StateListDrawable is a drawable object defined in XML that uses several different images to represent the same graphic, depending on the state of the object.
For example, a Button widget can exist in one of several different states pressed, focused, or neither and, using a state list drawable, you can provide a different background image for each state. The state list supports different view states such as android: The illustration below shows all the major states that can be represented:. Now, when the view i. Note that any view can have a state selector , but the most common uses are with buttons and listview items.
Using state lists allows us to easily define dynamic views that respond to pressed, checked, enabled or other related states. A LayerDrawable is a drawable object that manages an array of other drawables. Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top. The LayerList can be used to draw multiple other drawables shapes, images, etc and position them in relationship to one another.
The layers are placed on top of one another by default with the last item being drawn on the top. Layers can then have their coordinates shifted using the left , right , top , and bottom properties. Keep in mind that the items in a LayerList can also be images or any other type of drawable. You can use this to create more complex drawings and overlay multiple drawables on top of each other. See more examples within the official docs. A NinePatch is a PNG image in which you can define stretchable regions that are stretched when the content within the View exceeds the normal image bounds.
The most common use is a button that has to stretch based on the text displayed inside. NinePatch are images that have a. The file is no different from a normal PNG file except that you will add thin black lines to indicate the vertical and horizontal "stretchable" and "fill" areas for the image. Android will not show these guide lines, which are used to determine how the image should be rendered. An example of a 9-patch image is embedded below left is the 9patch graphic, right is an example of use in an app:. A NinePatch is defined and saved in the drawable folder and setting the background works the same as with any image:.
The left and top lines need to be defined for the stretchable regions. To avoid stretching the arrow of this speech bubble in the above example, we define regions outside this area. The right and bottom lines define where text can be filled. Without the bottom line for instance, your text will not fill up the entire width of the stretched region and may not be centered properly. You can also use the draw9patch tool to create special nine-patch images or you can use the online 9-patch utility.
You can check out great ready-made examples of pretty 9-patch buttons for examples.