Search code examples
androidkotlinandroid-roomcontacts

Android contact view style


Android contact view

I have a database with a column called PoemTitle, so I want to get the first letter from each title and give it a vector background with different colors. Please, can anyone help me with a function to do so depending on the char type? Below is the code that I tried:

fun getHeaderFirstChar(poems:Poems, res: Resources) {
  when (poems.PoemTitle.first()) {
    'a'..'b' -> res.getbackground
  }
}

That is the code I tried but it's not working Note: language is Kotlin and I'm using RoomDatabase


Solution

  • Don't re-invent the wheel, have a look at this library: https://github.com/amulyakhare/TextDrawable enter image description here

    and yes its Java but you can use it in Kotlin as well!