androidandroid-layoutandroid-button

How to change the color of a button?


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"/>

Solution

  • 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="#FFFFFF"
    

    Coding:

    //btn represents your button object
    
    btn.setBackgroundColor(Color.WHITE);
    btn.setTextColor(Color.BLACK);