How to set gradient background color in android studio

I was making a login layout like one one bellow


I am was making the background image in adobe illustrator but the i thought why not i should use the gradient option in android studio instead of using the background image that will also take some space in storage and it will have fixe colors. So i found on web the way How to set gradient background color in android studio and i found one way for it.

Let me share it with you so that you guys can also use it and it could help someone.

So what you have to do. you have to create a drawable resource file in you drawable folder buy going to drawable > new > and drawable resource file. Name your drawable resource file and hit enter.Boom!

Now what you have to do you have to obviously type some code in that file but don't worry about it. here is the code that you have to type there.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>        <shape>            <gradient                android:startColor="#4D3F60"                android:endColor="#C36F8A"                android:angle="90"></gradient>        </shape>    </item></selector>

Now just save your file by Ctrl+S. 

StartColor means the color at the starting of gradient
EndColor means the colot at the end of gradient
angle means orienttaion " top to bottom,bottom to top,left to right , right to left or you can set according to yours.

Now, It's time to use this resource file where you want this gradient too appear. So go to your xml file and write into the properties of widget 

android:background="@drawable/background"

you may be think that what me background , it's actually the name of my drawable resource file that i created in my android studio. Make sure to use the name of your file i.e ( you android:background="@drawable/name_of_your_file" )

That's it. If you found this article useful then make sure to support us by subscribing our YouTube channel by clicking on the button bellow. ( It's your choice because i cannot force you if you are not interested in coding related content ).


Previous
Next Post »

Popular Posts