0

I have a class with the following enum and variables:

enum class ExerciseSetState {
    NOT_STARTED, PASSED, FAILED
}

@Entity
class ExerciseEntity(
        ... 
        @Ignore var setState: ExerciseSetState = ExerciseSetState.NOT_STARTED
        ....
 )

And in my databinding layout, I want to set the color based on this state variable:

<TextView
        android:id="@+id/tv_exer_rep_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:text="@{String.valueOf(exercise.setReps)}"
        android:backgroundColor="@{exercise.setState == ExerciseSetState.PASSED? (Color.GREEN : exercise.setState == ExerciseSetState.FAILED ? Color.RED: Color.TRANSPARENT )}"/>

Unfortunately I have this pretty horrible ternary expression in order to compute the color based off the value.

What is the idiomatic way of implementing this?

1

3 Answers 3

2

Make it simple with adapter:

 @BindingAdapter("xmlTag")
        public static void bindColor(TextView textView, @Nullable ExerciseSetState state) {
            //switch state and set the value
        }

in xml:

app:xmlTag="@{exercies.setState}"
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. Where should I put the code for the binding adapter? It doesnt seem to fit in my WorkoutActivity or my WorkoutViewModel..
just create a separate class for it
I did that. Thx.
1

Create a Converter class and use it for converting enum to color

Layout:

<import type="*.Converter"/>
...

android:background="@{Converter.enumToColor(context, exercise.setState)}"

Converter Class:

class Converter {

    fun enumToColor(context: Context ,enum: ExerciseSetState) : Int {
        return when (enum){
            ExerciseSetState.NOT_STARTED -> getColor(context, R.color.colorAccent)
            ExerciseSetState.PASSED -> getColor(context, R.color.colorAccent)
            ExerciseSetState.FAILED -> getColor(context, R.color.colorAccent)
        }
    }
}

getColor helper method:

fun getColor(context: Context, colorId: Int): Int {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        context.resources.getColor(colorId, null)
    } else {
        context.resources.getColor(colorId)
    }
}

and better to change setState to getState in here :)

Comments

0

You can simply specify colors in the enum. Because colors relate to enum.

enum class ExerciseSetState(var color: Int) {
    NOT_STARTED(Color.TRANSPARENT), PASSED(Color.GREEN), FAILED(Color.RED)
}

and

android:backgroundColor="@{exercise.setState.color}"

1 Comment

And change setState to getState or state for better naming convention.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.