Gradient drawable in android PDF

Title Gradient drawable in android
Course Android Architecture
Institution Galgotias University
Pages 12
File Size 514.3 KB
File Type PDF
Total Downloads 74
Total Views 148

Summary

lecture and study notes...


Description

Gradient Drawable In Android In android, we can create Gradient drawable and use them as background resource for TextViews, Buttons, ListView etc. Gradient helps to make the the GUI better and stylish. Shapes also support gradients backgrounds as well supporting properties such as startColor, centerColor, endColor, angle. Different gradients such as radial, linear or sweep can be selected using the type property. If "drawable" folder is not in "res" folder, create a "drawable" folder inside "res" folder and put all the gradient drawable xml files in it. To create gradient color in android and use it as a background, we need to create an .xml file in the drawable directory. In XML file use tag to specify the properties of gradiant, Create a gradient.xml file in your drawable folder. In this gradient.xml we will define the color and the gradient. We do this by defining the start and end color and the number of degrees how the color need to change from the start value till the end value.



android:startColor: This is the starting color of the gradient. This color will start from the top of the screen. android:endColor: This is the ending color of the gradient. android:centerColor: This color will come in the center of the screen. android:angle: This is a special angle and works only with the multiple of 45 including 0. So you can give 0, 45, 90, 135 and so on. Depending on the angle gradient position will change on the screen.

You can use this gradient in layout file as background of a layout or any view. Now open the layout from your activity in which you want to set this gradient background. In our case, activity_main.xml. Set the android:background=”@drawable/gradient”, in the parent view, in our case the Relative layout.



Gradient

The different types of gradients that we can apply to shapes to set the colors for the outer, inner, and middle portions of the gradient color spectrum. The child tag inside parent tag is used in ShapeDrawable XML definition to specify the properties of gradient The android:startColor parameter to start the process of configuring the gradient for ShapeDrawable object. In our example of Oval ShapeDrawable, Set a hexadecimal color value of 100% red channel using #FF0000 in the android:startColor parameter, and use 100% yellow (red plus green) for the android:centerColor parameter using the hexadecimal value #FFFF00. For the android:endColor, use a 100% green hexadecimal color value, or #00FF00, so that you’re using colors that will blend well in your gradient. To specify the pivot point, or a center point as it is called with a gradient, using the android:centerX and android:centerY parameter both set to the exact middle (50%) of the shape, by using float values of 0.5. With these five parameters, the XML definition for the oval Shape with a linear (default) gradient of red, yellow, and green color values will look exactly like this following block of XML markup:



Examples of creating Gradients 1st Gradient Drawable Darker to Lighter shade

gradient_drawable1.xml





2nd Gradient Drawable Lighter to Darker shade

gradient_drawable2.xml





3rd Gradient Drawable Darker at Boundaries and Lighter in center

gradient_drawable3.xml





In below layout I have used these gradient drawable as background resource of Button....


Similar Free PDFs