amar_1995
11/12/2019, 9:26 AMtopAppBar
I wrote this sample code
TopAppBar(
title = { Text(text = "Sample App") },
navigationIcon = {
SimpleImageButton(R.drawable.hamburger) {
openDrawer()
}
},
actionData = listOf(+imageResource(R.drawable.search),+imageResource(R.drawable.img_translate))
) {
AppBarIcon(icon = it, onClick = { Toast.makeText(context, "Clicked ", Toast.LENGTH_SHORT).show()})
}
I do have list of two action item, now how to decide which action icon is clicked ? Is there is any way to check image type inside if-else
or when
statement.amar_1995
11/12/2019, 9:44 AMconditional
statement is working inside onClick method
AppBarIcon(icon = it, onClick = {
when(it) {
actionData[0] -> {
Toast.makeText(context, "Clicked Search", Toast.LENGTH_SHORT).show()
}
actionData[1] -> {
Toast.makeText(context, "Clicked Translate", Toast.LENGTH_SHORT).show()
}
}
})
Is there any better way to implement.amar_1995
11/12/2019, 9:47 AMZach Klippenstein (he/him) [MOD]
11/12/2019, 2:43 PMTopAppBar
kdoc:
For example, you may choose to represent an action with a sealed class containing an icon and text, so you can easily handle events when the action is pressed.