I want to add two action icon with label and navig...
# compose
a
I want to add two action icon with label and navigationIcon on
topAppBar
I wrote this sample code
Copy 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.
Using
conditional
statement is working inside onClick method
Copy code
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.
I want to do something as like the amazon image show. And on scroll amazon icon will disappear and seach bar will move on to that.
z
To make your code more type safe you could define an enum or sealed class for the actions. From the
TopAppBar
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.