Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android button with different background colors

i want to change the background-color of a button using a selector-xml-file. My approach is basically the one from the example at the bottom this page: http://developer.android.com/guide/topics/resources/color-list-resource.html

i have a res/color/button_text.xml which looks like this:

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">     <item android:state_pressed="true"           android:color="#ffff0000"/> <!-- pressed -->     <item android:state_focused="true"           android:color="#ff0000ff"/> <!-- focused -->     <item android:color="#ff000000"/> <!-- default --> </selector> 

and my layout contains the following code:

<Button     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/button_text"     **android:background="@color/button_text"** />  

(** is only there to show you that i use android:background instead of android:textcolor)

this code crashes. it says "Binary XML file line #4 tag requires 'drawable' attribute or child tag defining drawable. But if I try it with android:textColor as described in the above link it works fine. So it has to be the background issue. I don't want to create a 9patch-png if it's not necessary (basically i just need a "clickable" rectangle so i use a button with a colored background)

like image 967
Daniel Avatar asked Sep 17 '10 20:09

Daniel


People also ask

How can change background color of button in Android?

To set Android Button background color, we can assign android:backgroundTint XML attribute for Button in layout file with the required Color Value. To programmatically set or change Android Button background color, we may call pass the method Button.

How can I change background color of a 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 can I make my Android button more attractive?

Using colors (background, text, border) Using custom shapes like circle, rounded corners and more. You can add images to your buttons to customize them. Using drawables to make gradients, dotted borders and more.

Can we change the color of a button more than once?

Explanation: We can change by dyind it with different colours.

How to show a standard Android button with a different color?

This example demonstrate about Standard Android Button with a different color Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken button view to show different colors.

How to change the background color of a drawable in Android?

You can use android:background for a drawable. If you want just to change the color, use the app:backgroundTint attribute.

How do I add a background image to a green button?

Set your set your green button background image as the background, and your transparent png as the src. You can apply a selector to the background just the same as you would to a button also... EDIT: Actually there is an ImageButton already made for this, you should be using that instead.

What is a button in Android Studio?

A Button is a user interface that are used to perform some action when clicked or tapped. In this article, we will try to change the shape and color of Button to various designs, like: Please refer to this article to see in detail about how to create a new Android Studio project.


1 Answers

As your error states, you have to define drawable attibute for the items (for some reason it is required when it comes to background definitions), so:

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">     <item android:state_pressed="true" android:drawable="@color/red"/> <!-- pressed -->     <item android:state_focused="true" android:drawable="@color/blue"/> <!-- focused -->     <item android:drawable="@color/black"/> <!-- default --> </selector> 

Also note that drawable attribute doesn't accept raw color values, so you have to define the colors as resources. Create colors.xml file at res/values folder:

<?xml version="1.0" encoding="utf-8"?> <resources>      <color name="black">#000</color>      <color name="blue">#00f</color>      <color name="red">#f00</color> </resources> 
like image 98
Konstantin Burov Avatar answered Oct 07 '22 16:10

Konstantin Burov