Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"/> 
like image 896
hahoanghai.ueh Avatar asked Sep 19 '15 17:09

hahoanghai.ueh


People also ask

How do I change the color of my click button?

To change the background color of the button, use the CSS background-color property and give it a value of a color of your taste. In the . button selector, you use background-color:#0a0a23; to change the background color of the button.

How do you change the color of a button Excel?

To do this, in the Ribbon, go to Developer > Properties. 2. In the Properties window, (1) click on Alphabetic and (2) select ForeColor. After that, (3) click on the arrow to the right to display the color options, and (4) choose a color from the Palette (or System).


2 Answers

The RIGHT way...

The following methods actually work.

if you wish - using a theme
By default a buttons color is android:colorAccent. So, if you create a style like this...

<style name="Button.White" parent="ThemeOverlay.AppCompat">     <item name="colorAccent">@android:color/white</item> </style> 

You can use it like this...

<Button     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:theme="@style/Button.White"     /> 

alternatively - using a tint
You can simply add android:backgroundTint for API Level 21 and higher, or app:backgroundTint for API Level 7 and higher.

For more information, see this blog.

The problem with the accepted answer...

If you replace the background with a color you will loose the effect of the button, and the color will be applied to the entire area of the button. It will not respect the padding, shadow, and corner radius.

like image 132
Nechemia Hoffmann Avatar answered Sep 25 '22 10:09

Nechemia Hoffmann


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); 
like image 42
Aicha Hezit Avatar answered Sep 25 '22 10:09

Aicha Hezit