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)
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.
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.
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.
Explanation: We can change by dyind it with different colours.
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.
You can use android:background for a drawable. If you want just to change the color, use the app:backgroundTint attribute.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With