Coloring buttons in Android with AppCompat

Does anyone know how to change the color of a button?

but one specific button, and not all application buttons using XML.

The android: backgroundTint attribute does not work on pre-Lollipop even with the AppCompat library. Only colorButtonNormal from the theme runs on pre-candy.

It's true? what a disgrace

I use this drawable (plain green color) with a button, but when I use a button, it turns out to be higher than a regular button.

this is the btn_green.xml file

<?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> 

button:

  <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 drawable, the button is higher than usual (without drawing), I donโ€™t know why.

+6
source share
2 answers

There is a way to install BackgroundTint on devices with pre-Lellipop. Try this: button.setSupportBackgroundTintList(getResources().getColorStateList(R.color.accentColor)); Pay attention to this answer: The Lollipop background tag does not affect the button

+6
source

if you want below style

enter image description here

add this style to your button

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

if you want this style

enter image description here

add code below

 style="@style/Widget.AppCompat.Button.Colored" 
+9
source

Source: https://habr.com/ru/post/988582/


All Articles