Tuesday, February 19, 2019

Gradients in android

How to create gradient in Android Studio?


Here, we are talking about a drawable with a gradient for buttons, backgrounds,etc. So, it is basically an XML document which defines background - color effects for buttons and backgrounds. First of all, you will need android studio installed on your system. Then, create a new project in which you want to provide background - color effect to any particular button. Here, I have already created a project. So I will directly show you required coding snippets. 

First of all, in your project structure, create a new drawable resource file (XML file) in following path: res → drawable by right clicking on drawable and then select New → Drawable resource file as shown below.







After creating new resource file, replace root element i.e. <selector> with <shape>. shape element is having several attributes like, startColor, endColor, centerColor, angle, etc. You can find more information about it on https://developer.android.com/ Now, in your newly XML file apply changes as following,






Now, to apply this background effect, in activity_main.xml set  the background property as "@drawable/filename". For example,





So, it will generate following effect to background of your activity_main.xml






Same, procedure one will have to follow if he/she wants to apply background effects to buttons or image buttons.

Hope you like it!!! :D 


No comments:

Post a Comment