Home > Tricks & Tips > Setting button’s colors

Setting button’s colors

For a button, you can either set its colors by using layout setting xml in resources, or dynamically through coding.

1. Using XML resources

– To set button background color, use: “android:background”

– To set button text color, use: “android:textColor” (note, it’s case-sensitive)

Sample: to create a button with background “WHITE” and text color “BLACK”


2. Through coding

– To set button background color, use: “Button.setBackgroundColor()”

– To set button text color, use: “Button.setTextColor()”

Sample: to create a button with background “WHITE” and text color “BLACK”


3. Sample Project

This is a small project to demonstrate the usage.

A button with BLACK background and WHITE text color, if it’s clicked the color reverse; if it’s clicked one more time, its colors transforms to the beginning color state.

First State

First State

After clicked

End State

End State

Get the sample project here: FileDropper | FreeFileHosting | FileSave | Mediafire

4. Final Words

– Hope you enjoy it!


Pete Houston

Categories: Tricks & Tips Tags: , , ,
  1. November 28, 2014 at 9:50 am

    Howdy would you mind letting me know which webhost you’re
    utilizing? I’ve loaded your blog in 3 completely different browsers
    and I must say this blog loads a lot quicker
    then most. Can you recommend a good internet hosting provider at a fair price?
    Thanks, I appreciate it!

  2. November 24, 2014 at 2:09 pm

    Nice post and thanks for share any way . I want you to visit the link below to get some useful info like Customer Care Number

  3. Ram
    October 14, 2014 at 4:12 pm

    really helpful

  4. August 3, 2014 at 2:37 am

    It is in reality a nice and helpful piece of info.
    I’m satisfied that you simply shared this useful info with us.
    Please keep us informed like this. Thank you for sharing.

  5. March 17, 2014 at 10:48 pm

    I’ve been exploring for a bit for any high quality articles or blog posts in this kind
    of area . Exploring in Yahoo I eventually stumbled upon this site.
    Studying this info So i’m satisfied to convey that I’ve an incredibly excellent uncanny feeling I came upon exactly
    what I needed. I most definitely will make sure to do not
    disregard this web site and provides it a look regularly.

  6. February 14, 2014 at 8:00 am

    It’ll also save a lot of cash and time for those on a restricted budget who want
    to successfully expand their companies. Article Source: Media is a creative design group that specializes in Video Marketing,
    Website Design, Search Engine Optimization, Graphic Design, and Social Media Marketing.
    Procedures to developing a cell phone game start by creating a plan, followed by creating the
    artwork, then developing your own gameplay, next is combining
    the artwork and gameplay, and lastly is by posting your brand new game on the web for income.

  7. June 10, 2013 at 9:36 am

    This is a great tutorial. I’ve just followed your blog and hope to learn lots. I’m learning to develop Android apps right now and resources like this are perfect.

  8. May 9, 2013 at 9:44 pm

    Then wrap the fabric tightly in plastic and maintain it airtight for two months.
    Oz indicates saffron extract to suppress your urge for food.

  9. May 8, 2013 at 4:58 pm


    When i used btnSample.setBackgroundColor(Color.WHITE); code to apply color to my button, It automatically increases the size of the button. Do you know why it was happening?

  10. May 6, 2013 at 5:27 pm

    I like what you guys are up too. This sort of clever work and coverage!
    Keep up the very good works guys I’ve included you guys to blogroll.

  11. mutasem
    February 16, 2013 at 5:55 pm

    Like toy tutorials🙂

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: