1

I'm trying out the Android Slices and found that there is no way to build the Slice Row with setTitleItem only. I want to create the Slice as like the attached screenshot. But it appears that setTitleItem method not working.

Artifact used implementation 'androidx.slice:slice-builders-ktx:1.0.0-alpha4

return list(context, sliceUri, ListBuilder.INFINITY) {
            row {
                setTitleItem(createActivityAction(Intent(context, MainActivity::class.java), R.drawable.ic_pizza_slice_24, SliceHints.ICON_IMAGE))
                title = "Welcome Android Slice"
                subtitle = "It has Start Item"
            }
        }

I've opened a Google Issue but it's not helpful.

Please let me know if anybody knows what I'm doing wrong here.

The code is here:

Github Code

enter image description here

Pritam
  • 339
  • 3
  • 23
sam_k
  • 5,983
  • 14
  • 76
  • 110

2 Answers2

0

At the moment, the first row of a Slice cannot have a start item. As Slices are still in development this could be subject to change in the future. Using setTitleItem() to set a start item in following rows should work:

return list(context, sliceUri, ListBuilder.INFINITY) {
    row {
        title = "Welcome Android Slice"
        subtitle = "Header row"
    }
    row {
        setTitleItem(createActivityAction(Intent(context, MainActivity::class.java), R.drawable.ic_pizza_slice_24, SliceHints.ICON_IMAGE))
        title = "2nd row"
        subtitle = "It has Start Item"
    }
}
AdamK
  • 21,199
  • 5
  • 42
  • 58
  • Thanks for an alternative answer. Here is the issue to track: https://issuetracker.google.com/u/1/issues/112472535 – sam_k Sep 11 '18 at 22:53
0

When there is no header, the first-row item automatically becomes header. As the header does not have an option for setTitleItem(), the icon is not displayed in the left corner.

In your case, the reason for the icon being displayed on the right corner is due to the fact that header's primary action is displayed on the right side

Take a look at the appendix of this document they have mentioned You can have a header, but if you don't, the first item becomes automatically a header

Also take a look at this google issue tracker

In the future, there might be a possibility of support for setTitleItem() in the header of android slice.

Raghul Vaikundam
  • 588
  • 4
  • 20