seba123neo
seba123neo

Reputation: 4748

Coloring Buttons in Android with AppCompat

Does anyone know how to change the button color?

but one particular button, not all the buttons of the application using the XML.

The android:backgroundTint attribute doesn't work on pre-Lollipop even with AppCompat library. Only colorButtonNormal from theme works on pre-Lollipop.

Is that true? what a shame

I'm using this drawable (is a simple green color) with a button, but when I use the button turns out to be higher than the normal button.

this is the file btn_green.xml

<?xml version="1.0" encoding="utf-8"?>

<item android:state_pressed="true"><shape>
        <solid android:color="#ff5722" />

        <corners android:radius="4dp" />
    </shape></item>
<item android:state_focused="true"><shape android:shape="rectangle">
        <solid android:color="#4caf50" />

        <corners android:radius="4dip" />

        <stroke android:width="1dip" android:color="#F0FC00" />
    </shape></item>
<item><shape>
        <solid android:color="#4caf50" />

        <corners android:radius="4dp" />
    </shape></item>

the button is this:

            <android.support.v7.widget.AppCompatButton
            android:id="@+id/btnIngresar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_green"
            android:text="Hello"
            android:textStyle="bold" />

when i use the drawable, the button is higher than the normal button (without the drawable), i don't know why.

Upvotes: 6

Views: 3829

Answers (2)

Jinu
Jinu

Reputation: 8675

if you want below style

enter image description here

add this style your button

style="@style/Widget.AppCompat.Button.Borderless.Colored"

if you want this style

enter image description here

add below code

style="@style/Widget.AppCompat.Button.Colored"

Upvotes: 9

Mateus Brandao
Mateus Brandao

Reputation: 900

There's a way to set the BackgroundTint in pre-lollipop devices. Try this: button.setSupportBackgroundTintList(getResources().getColorStateList(R.color.accentColor)); Refer to this Answer: Lollipop's backgroundTint has no effect on a Button

Upvotes: 6

Related Questions