change colour of button in android

Change colour of button in Android Studio

As you know, the Default color of the button in the android studio is grey which is not suits for UI. So, here we are going to discuss how to change the colour of the button in android studio using different methods

Method 1: Using XML

First, add the button in XML layout like this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GeeksToCode"
android:layout_gravity="center"
android:layout_marginTop="200dp"
/>
</LinearLayout>
change colour of button in android
Normal color of button

After this you will see your button looks something like this

Now you need to add another tag that is android:background=”color Hexa code” like this,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
   android:orientation="vertical"
    android:layout_height="match_parent">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GeeksToCode"
        android:layout_gravity="center"
        android:background="#FF5722"      
        android:layout_marginTop="200dp"
        />
</LinearLayout>

Now, you see your button looks like this

change colour of button in android
coloured color of button

Method 2: Using a drawable file

This is my best method because it provides you many other operations to perform on the button like rounded corner etc.

Right click on drawable -> new-> drawable resource file and named as background.

Add following line of code there

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >
    <stroke android:width="1dp" android:color="#FFC107" />
    <solid android:color="#FFC107"/>
</shape>

You see something like this

change colour of button in android
drawable file

Now, goto your button and drawable like this

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GeeksToCode"
        android:layout_gravity="center"
        android:background="@drawable/rounded"
        android:layout_marginTop="200dp"
        />
change colour of button in android
coloured button

We update this content as we see more and more methods to change the colour of button in android studio.

How to add button in android studio?

Simply add from the design section of XML layout.

How to change the colour of button in android studio?

Add android:background=”colour” in button

Sudhanshu is Technology geek and also a pro pubg player. I like to create content in my free time that helps others. Currently pursuing BCA from Noida and operating Geekstocode

Leave a Comment

Your email address will not be published. Required fields are marked *