Home > Software design >  How to change background color dynamically in Android
How to change background color dynamically in Android

Time:08-19

In my application I have 10 views, I want when click on each item change background color to for example yellow and other view background color is blue.
My views name is, for example : item1Line, item2Line, item3Line and ... item10Line .
I think I can control the views with the loop command and the conditional command.
I write below codes :

private fun lineColorSelected(item: Int) {
    //Line colors
    for (i in 1..10) {
        if (item == i) {
            view.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.yellow))
        } else {
            view.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.blue))
        }
    }
}

Note : In above code, view is sample.
Bu I don't know how can I add my views to list and use this in if condition!

Update: Normal way I write below code :

    private fun selectedLine(item: Int) {
        binding.apply {
            when (item) {
                1 -> {
                    item1Line.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.meatBrown))
                    item2Line.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.pictonBlue))
                    item3Line.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.pictonBlue))
                }
                2 -> {
                    item1Line.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.pictonBlue))
                    item2Line.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.meatBrown))
                    item3Line.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.pictonBlue))
                }
                3 -> {
                    item1Line.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.pictonBlue))
                    item2Line.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.pictonBlue))
                    item3Line.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.meatBrown))
                }
    }
}

But in this way for 10 views is very bad way.
I want use for and if condition for handle above code with best practice.

How can I it?

CodePudding user response:

A simple way is

// using run so we don't need to go binding.item1Line etc, and the list gets returned
val myItems = binding.run {listOf(
    item1Line, item2Line, item3Line, item4Line, item5Line,
    item6Line, item7Line, item8Line, item9Line, item10Line
)}

Then you can do your colour setup with:

private fun lineColorSelected(item: Int) {
    myItems.forEachIndexed { i, line ->
        val colour = if (i == item) R.color.meatBrown else R.color.pictonBlue
        line.setBackgroundColor(ContextCompat.getColor(requireContext(), colour)
    }
}

When you have a nice list of views like this, you can do things like set up click listeners easily too:

myItems.forEachIndexed { i, line ->
    line.setOnClickListener { lineColorSelected(i) }
}

(I'm treating the first item as 0 since I'm using indices, your code uses 1 so be careful of that if you adapt this)

  • Related