Android/Kotlin Material Button checkedId, what is it?

Wonder if anyone can tell me what exactly the checkedId of a Material Button is? The documentation is not very clear. For instance:

button.addOnButtonCheckedListener { group, checkedId, isChecked -> stuff e.g. Toast}

For the checkedId, I get a long integer. For instance, I have a button with XML ID android:id="@+id/paletteF", and when I click it the checkedId returns as 2131362134. I don’t see an obvious correlation here, if there is one… Is it an address??

Answer

First it is useful to understand that Android has a generated class called R which contains identifiers for all of your XML declared resources. So every time you declare @+id/newId in an XML file, a new static field (an integer) is created in the R class representing that ID, namely R.id.newID.

So in answer to your question, the checkedId refers to the same ID that android:id="@+id/paletteF" does and to check them programmatically you would would something like:

checkedId == R.id.paletteF

Just as a side note, this was an oversimplification of the R class but if you’re interested there’s plenty of information out there, some of it on this forum.

Leave a Reply

Your email address will not be published. Required fields are marked *