How to customize button in android
Hello, Today we are going to see how we can customize the button in android.
We will see how we can make a rounded corner button with background color
and also see how we can gradient to the button.
See the below gif for both examples.
Gradient Button in Android |
Background Color button in Android |
First, let's see
How to change button background color in android
Follow these steps to change your button background in android.
To apply or change the background color of a button in android, we need to
make a button in our layout and a new drawable file.
We have added our button in layout as below,
we will make a drawable file and write the code for the background of the
button.
After creating the file. Add this code in the drawable file, here we have
named this file as bg_btn.xml.
In the above code, first, we specify the shape of the button by setting the root element as shape and pass attribute shape and set its value to rectangle. Ater the shape, we added a child element solid and set attribute color to red.
Now we have created our button background, now we have to apply these
settings to the button.
Open your activity layout file in the layout folder and pass a background
attribute in a button child and set its value to that drawable file we
have made earlier, see below code for reference.
We have added some padding and change the text color to white so that it
will look good and attractive button in android
Now you can view your custom button preview in the preview window and if you run
your app on the android mobile, you will see your custom button.
Now let's see how we can make a rounded corner button in android.
Custom Rounded Corner Button in Android
To make the rounded corner button in android, we need to add a new child
element in our drawable file which we have made before.
Open up your drawable file and add this code:
This new child element in our drawable file is pretty self-explanatory, as you
can see we have added corner element and set its radius attribute to 15sp to
make button rounded corner in app.
We don't need to change anything in the activity layout file,
Now run your app and you will see your rounded corner button in android.
But isn't this button looks boring?
What about to add the gradient color in the button. Sounds goods right?
How to make Gradient Button in Android
To make the gradient button in android we need to edit our bg_btn.xml file.
Open your file and add this code:
As you can see a new child element gradient and it has 2 properties or
attributes.
The first attribute is for start color of gradient and the second is for the
second color for the button.
We set the value as hex color, you can search on the internet for different
colors you like.
Now our button looks more attractive and professional.
Hope you learned from this article. If you enjoy this article do share on
Facebook, on Whatsapp, share it with your friends and your mates.
Thank you for your time. Happy Coding. :)