19

The backgroundTint is correctly applied on API 23, but not on API 19. How can I get the drawable tinted for API 19 and below?

                    <Button
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:id="@+id/AbResetBtn"
                    android:background="@android:drawable/stat_notify_sync"
                    android:backgroundTint="@color/button_material_light" />

Of course my Activity extends AppCompatActivity.

xarlymg89
  • 2,552
  • 2
  • 27
  • 41
ema3272
  • 1,021
  • 2
  • 13
  • 28

4 Answers4

36

This worked for me on API19 device, support lib v7

layout

<Button
    android:id="@id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/label"
    style="@style/Button"
    />

styles

<style name="Button" parent="Base.TextAppearance.AppCompat.Button" >
    <item name="backgroundTint">@color/fab_bg</item>
</style>
DoruChidean
  • 7,941
  • 1
  • 29
  • 33
  • 1
    This is a more elegant solution than the accepted answer - it avoids the need for setting colours programmatically, doesn't need to reference AppCompatButton directly, and keeps all required layout code in the styles and other XML files. Neat and tidy. – BasicPleasureModel Jun 05 '17 at 15:49
  • You are right. I have tried this more recent solution and it works, too. – ema3272 Sep 03 '17 at 14:04
9

I know its little bit old question but you don't need to create a style element even.

Just use AppCompatButton from the support library with app: namespace.

<android.support.v7.widget.AppCompatButton android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:id="@+id/AbResetBtn"
                    android:background="@android:drawable/stat_notify_sync"
                    app:backgroundTint="@color/button_material_light" />
Paresh Dudhat
  • 1,166
  • 1
  • 14
  • 28
5

You need to use android support library 22.1+ to use AppCompatButton http://android-developers.blogspot.se/2015/04/android-support-library-221.html

But unfortunately you will not be able to do this in the xml.

In the onCreate of your activity, to the following:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AppCompatButton v = (AppCompatButton) findViewById(R.id.mybutton);
        ColorStateList csl = new ColorStateList(new int[][]{new int[0]}, new int[]{0xffffcc00});
        v.setSupportBackgroundTintList(csl);
    }
}

More info here: Lollipop's backgroundTint has no effect on a Button

Tip: maybe you will be able to do everything in the xml using app:backgroundTint="@color/button_material_light", but I didn't tested.

--EDIT--

Check @ema3272 second comment for the full solution

Community
  • 1
  • 1
jonathanrz
  • 4,206
  • 6
  • 35
  • 58
  • I use Support Library 23.1.1. In fact this button is included in a view that is inflated in a Fragment (it is a "help" screen). I already tried the above code in the "onCreateView" in the Fragment class. However, I get a compile error that says "unexpected cast to AppCompatButton: layout tag was Button". By the way, the issue is already present when previewing the "help" screen in Android Studio. And the XML tip above does not work either. – ema3272 Dec 05 '15 at 20:11
  • 1
    @ema3272 change to AppCompatButton in your xml too – jonathanrz Dec 06 '15 at 12:34
  • OK. This solved the issue. However, converting from Button to AppCompatButton is not enough, you have to use android.support.v7.widget.AppCompatButton in XML and the above code to get the correct result. The official documentation at http://developer.android.com/intl/pt-br/reference/android/support/v7/widget/AppCompatButton.html says "This (class) will automatically be used when you use Button in your layouts. You should only need to manually use this class when writing custom views", which is not correct. – ema3272 Dec 06 '15 at 16:36
0

You have to update a "Button" to a "androidx.appcompat.widget.AppCompatButton" and "android:backgroundTint" to "app:androidTint"

Before:

 <Button
                android:id="@+id/button"
                android:layout_width="200dp"
                android:layout_height="0dp"
                android:textColor="@color/colorAccent"
                android:backgroundTint="@color/colorAccent"
                android:background="@drawable/empty_list_state_button"
                android:text="@string/button_title"
                app:layout_constraintTop_toBottomOf="@id/distance"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="16dp"
                />

After:

 <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/button"
                android:layout_width="200dp"
                android:layout_height="0dp"
                android:textColor="@color/colorAccent"
                app:backgroundTint="@color/colorAccent"
                android:background="@drawable/empty_list_state_button"
                android:text="@string/button_title"
                app:layout_constraintTop_toBottomOf="@id/distance"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="16dp"
                />
Blazej SLEBODA
  • 8,936
  • 7
  • 53
  • 93