How to change the color of a button? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to change the color of a button? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m new to android programming. How do I change the color of a button?

<Button
    android:id="@+id/btn"
    android:layout_width="55dp"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:text="Button Text"
    android:paddingBottom="20dp"/>

Answer

You can change the colour two ways; through XML or through coding. I would recommend XML since it’s easier to follow for beginners.

XML:

<Button
    android:background="@android:color/white"
    android:textColor="@android:color/black"
/>

You can also use hex values ex.

android:background="@android:color/white"

Coding:

//btn represents your button object

btn.setBackgroundColor(Color.WHITE);
btn.setTextColor(Color.BLACK);
We are here to answer your question about How to change the color of a button? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji