Home > Back-end >  android:backgroundTint isn't working - Android
android:backgroundTint isn't working - Android

Time:10-21

Can someone please help me figure out why is the backgroundTint for the button not working? I use android:background for the background color and android:backgroundTint for the button background. But it seems like the BackgroundTint is not working (the button background color is set to default).

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@ id/tvCounter"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/default_background"
        android:gravity="center"
        android:text="0"
        android:textColor="#FFFFFF"
        android:textSize="200sp"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/tvCounter">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:id="@ id/button8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:backgroundTint="@color/black"
                android:background="@color/black"
                android:onClick="gantiBackground"
                android:text="HITAM"
                android:textColor="#FFFFFF"/>
            <Button
                android:id="@ id/button9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:backgroundTint="@color/red"
                android:background="@color/red"
                android:onClick="gantiBackground"
                android:text="MERAH"
                android:textColor="#FFFFFF"/>
            <Button
                android:id="@ id/button7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:backgroundTint="@color/blue"
                android:background="@color/blue"
                android:onClick="gantiBackground"
                android:text="BIRU"
                android:textColor="#FFFFFF"/>
            <Button
                android:id="@ id/button6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:backgroundTint="@color/green"
                android:background="@color/green"
                android:onClick="gantiBackground"
                android:text="HIJAU"
                android:textColor="#FFFFFF"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            <Button
                android:id="@ id/button10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="tambahCounter"
                android:text="TAMBAH"/>
            <Button
                android:id="@ id/button11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="reset"
                android:text="RESET"/>
        </LinearLayout>
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

Use

android:backgroundTint="@color/blue"
android:background="@color/red"

InsteadOf

android:backgroundTint="@color/blue"
android:background="@color/blue"

Try Different Color For "background" And "backgroundTint"

You Use Same Color for "background" And "backgroundTint"..That's Why "backgroundTint" look like does not work...

CodePudding user response:

Use app:backgroundTint instead of android:backgroundTint

  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@ id/button8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:backgroundTint="@color/black"//edited line
            android:background="@color/black"
            android:onClick="gantiBackground"
            android:text="HITAM"
            android:textColor="#FFFFFF"/>
        <Button
            android:id="@ id/button9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:backgroundTint="@color/red"//edited line
            android:background="@color/red"
            android:onClick="gantiBackground"
            android:text="MERAH"
            android:textColor="#FFFFFF"/>
        <Button
            android:id="@ id/button7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:backgroundTint="@color/blue"//edited line
            android:background="@color/blue"
            android:onClick="gantiBackground"
            android:text="BIRU"
            android:textColor="#FFFFFF"/>
        <Button
            android:id="@ id/button6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:backgroundTint="@color/green"//edited line
            android:background="@color/green"
            android:onClick="gantiBackground"
            android:text="HIJAU"
            android:textColor="#FFFFFF"/>
    </LinearLayout>
    
  • Related