0

I have scoured SO and nothing that I am finding has been able to fix this problem. I am trying to change the background drawable of a TextView depending on the text in the view using data-binding.

TextView.xml

<?xml version="1.0" encoding="utf-8"?>
<layout 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">
    <data>

       <variable name="data" type="package.name.Exercise" />

       <variable name="playTime" type="org.joda.time.Instant" />

       <import type="package.name.util.Converter" />
       <import type="android.view.View"/>
       <import type="package.name.R" />
    </data>

    <com.google.android.material.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="16dp"
        android:layout_marginVertical="8dp"
        android:contentDescription='@{data.title.localized() + ", " + data.description.localized()}'
        app:accClickHint="@{@string/acc_exercises_hint}"
        app:cardCornerRadius="8dp"
        app:cardElevation="2dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/icon"
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:layout_marginStart="10dp"
                android:layout_marginTop="30dp"
                android:importantForAccessibility="no"
                android:scaleType="fitCenter"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@id/title"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@{data.icon}"
                tools:srcCompat="@sample/exercise_icon" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:gravity="right">

                        <TextView
                            android:id="@+id/capacity1"
                            android:layout_width="75dp"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="7.5dp"
                            android:layout_marginBottom="4dp"
                            android:gravity="center"
                            android:textAppearance="@style/Small"
                            android:text="@{data.capacity.get(0).title()}"
                            android:background="@{data.capacity.get(0).color()}"/>

                      <!--More views here...-->

There is more to TextView.xml but it wouldn't affect the TextView. The data.capacity.get(0).color() comes from Exercise.java. I have used breakpoints and the drawables are being returned.

Exercise.java

public abstract class Exercise implements ExerciseListObject {

    public enum Capacity {
        THINK, FEEL, CTRL, EMO;

        public String title() {
            switch (this) {
                case THINK:
                    return "Cognition";
                case FEEL:
                    return "Feeling";
                case CTRL:
                    return "Self Control";
                case EMO:
                    return "Emotion";
            }
            throw new RuntimeException("Not a recognized case: " + this);
        }

        public int color() {
            switch (this) {
                case FEEL:
                    return R.drawable.shape_rounded_rectangle_4px_feeling;
                case THINK:
                    return R.drawable.shape_rounded_rectangle_4px_cognition;
                case CTRL:
                    return R.drawable.shape_rounded_rectangle_4px_control;
                case EMO:
                    return R.drawable.shape_rounded_rectangle_4px_emo;
            }
            throw new RuntimeException("Not a recognized case: " + this);
        }
    }
    
    // More irrelevant code here ...
}

shape_rounded_rectangle_4px_feeling.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners
        android:bottomLeftRadius="4dp"
        android:bottomRightRadius="4dp"
        android:topLeftRadius="4dp"
        android:topRightRadius="4dp" />

    <solid android:color="@color/feelingAccordion" />

</shape>

The problem is that it will not set the background correctly. It sets a background that looks like this: enter image description here

The background should be this color:enter image description here

Any idea what is going on? Thanks in advance!

ndsmith
  • 37
  • 8

2 Answers2

1

I figured it out thanks to this site: https://medium.com/swlh/android-data-binding-in-views-for-theming-your-app-580c0bf06c18

I needed to put this method in the databinding adapters file.

@BindingAdapter("android:background")
public static void setTextViewBackground(TextView view, int resource){
    view.setBackgroundResource(resource);
}

Without this method, the data binding library has no idea what to do with the background attribute.

ndsmith
  • 37
  • 8
0

try backgroundTint , can solve your problem when you want to change a background color without create a style for that use this but if your problem not solve tag me to tell you another way

MHmansouri
  • 73
  • 10