Cycle through an array of available colors repeatedly I have an array of colors for my app to choose from. My goal was to have the app cycle through the array of colors, apply it to my object, and reset to the beginning once each color has been used. This is simple enough to implement, but I am looking to see if it's possible to clean it up slightly. The one complication I have is that I am trying to prevent duplicate colors. 

E.g. If I choose to use the size of the array of the object that has the color affected - when an item (or multiple) is deleted and a new one is added, it might have a duplicate color as the previous card leading to a visual design that I don't want. 

To prevent this I decided to make a new counter variable that counts each time a new card is created, regardless of deletions. I feel like there might be a more elegant solution that I am not thinking of so any feedback or alternative approaches would be very appreciated!

All of the necessary code can be seen here:

class BrowseHabitsActivity : Activity() {
    private lateinit var recyclerView: RecyclerView
    private lateinit var addHabitButton: FloatingActionButton
    private val habits: ArrayList&lt;Habit&gt; = Habit.createHabitList(25)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_browse_habits)

        recyclerView = findViewById(R.id.rvHabits)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = BrowseHabitsAdapter(habits)

        var cardCount = 0

        addHabitButton = findViewById(R.id.addHabitButton)
        addHabitButton.setOnClickListener {
                habits.add(Habit("New Habit", 1234, chooseCardColor(cardCount++)))
                (recyclerView.adapter as BrowseHabitsAdapter).notifyItemInserted(habits.size)
        }
    }


    private fun chooseCardColor(cardCount: Int): Int {
        val colors = resources.getIntArray(R.array.cardColors)
        return colors[cardCount % colors.size]
    }
}```
The code you've written seems to be reasonably elegant (to me at least!). You can make this slightly simpler by:

storing the colors array rather than getting each time (I've used a by lazy but you could use a nullable var or a lateinit var if you prefer)
Store the cardCount elsewhere rather than locally and incrementing it each time

I've put an example of these changes below. I would strongly recommend extracting out the color logic into its own class in order to make it more testable, reusable and better separation of concern.
private val colors: IntArray by lazy{
    resources.getIntArray(R.array.cardColors)
}

private val cardCount = 0

private fun getCardColor(): Int {
    val color = colors[cardCount % colors.size]  
    cardCount.inc()
    return color
} 

Also, you could use the @ColorInt annotation to make it clear that the Int being returned is a ColorInt.