Button
1. Feature Description
RemoteButton is a clickable button component, corresponding to ViewType.BUTTON. It supports text styles (size, color, bold/italic, alignment, decoration), background shape (fill, stroke, corner radius), disabled state, and click events. It can be used to build common interactions such as filled buttons, stroke buttons, text buttons, and TabLayout.
Create it via RemoteViewHelper.createButton():
val button = helper.createButton()
2. Common Properties
The following properties are set via the corresponding setXxx() methods. Unless noted otherwise, if the component is already mounted, call syncToRemote() after changing a property to make it take effect on Pilot.
Size & Layout (inherited from RemoteViewProvider)
| Method | Params | Description |
|---|---|---|
setWidth | Int | Width (dp); special values RemoteView.MATCH_PARENT(-1) / WRAP_CONTENT(-2) |
setHeight | Int | Height (dp); same special values |
setMinSize | minWidth: Int, minHeight: Int | Minimum size (dp) |
setPadding | left, top, right, bottom: Int | Padding (dp) |
setVisibility | VisibilityType | Visibility: VISIBLE / INVISIBLE / GONE |
setAlpha | Float | Opacity, 0.0 – 1.0 |
setEnabled | Boolean | Whether enabled; disabled does not respond to clicks |
Background & Shape (inherited from RemoteViewProvider)
| Method | Params | Description |
|---|---|---|
setBackground | Int | Solid background color (ARGB Int) |
setBackgroundShape | ShapeDescriptor | Background shape: fill / stroke / corner radius, see ShapeDescriptor below |
ShapeDescriptor.Builder composable items:
| Builder method | Params | Description |
|---|---|---|
fillColor | Int | Fill color (ARGB) |
stroke | widthDp: Int, color: Int | Stroke width and color |
cornerRadius | dp: Float | Uniform corner radius |
cornerRadii | FloatArray(8) | Independent corners (top-left xy, top-right xy, bottom-right xy, bottom-left xy) |
shape | SHAPE_RECTANGLE / SHAPE_OVAL | Rectangle or oval |
Text Properties
| Method | Params | Description |
|---|---|---|
setText | String | Button text |
setTextSize | Float | Text size (rounded to sp internally) |
setTextColor | Int | Text color (ARGB Int) |
setFontStyle | FontStyleType | Font style: NORMAL / BOLD / ITALIC / BOLD_ITALIC |
setGravity | GravityType | Alignment: CENTER / START / END / LEFT / RIGHT / TOP / BOTTOM, etc. |
setMaxLines | Int | Max number of lines |
setSingleLine | Boolean | Whether single line |
setEllipsize | EllipsizeType | Ellipsis position: START / MIDDLE / END / MARQUEE |
setTextDecoration | TextDecorationType | Decoration: NONE / UNDERLINE / STRIKE_THROUGH / UNDERLINE_AND_STRIKE_THROUGH |
setLetterSpacing | Float | Letter spacing |
setLineSpacing | extra: Float, multiplier: Float | Line spacing (extra + multiplier) |
Events
| Method | Description |
|---|---|
setOnClickListener | Click callback. Runs on the Binder thread pool — switch to the main thread yourself for UI/business |
3. Tips
- Colors must be ARGB Int.
setTextColor/setBackgroundaccept an Int color value (e.g.0xFF2D8CF0.toInt()orColor.RED); the underlyingupdateViewconverts it to a decimal string. Do not pass"#RRGGBB","0xAARRGGBB", orR.color.xxx. - The click callback is not on the main thread.
setOnClickListener's callback runs on the AIDL Binder thread pool. To update your App's own Views or do main-thread work, switch back withHandler(Looper.getMainLooper()).post { ... }. - Call
syncToRemote()after changing a mounted component's properties.setXxxonly updates the local cache;syncToRemote()batch-syncs to Pilot. If you configure properties before mounting, no extra call is needed. - Dim the disabled state manually.
setEnabled(false)only stops responding to clicks; visually, also callsetAlpha(0.6f)to convey the disabled look. - Stroke button = no fill. Call only
stroke(...)withoutfillColorto get a hollow stroke effect; for a text button, do not callsetBackgroundShape. - Setting width to
0with a parent containerweightachieves even / proportional distribution (see the TabLayout example).
4. Sample Code
In each group below, the code sample is on the left and the corresponding Pilot-side rendered preview is on the right.
01 Component Types (basic / combined buttons)
// Basic buttons: filled / stroke / text
val filled = helper.createButton().apply {
setText("Filled")
setWidth(RemoteView.MATCH_PARENT); setHeight(32); setTextSize(14f)
setTextColor(RemoteViewColors.WHITE); setFontStyle(FontStyleType.BOLD)
setBackgroundShape(
ShapeDescriptor.Builder().fillColor(RemoteViewColors.BLUE_PRIMARY).cornerRadius(4f).build()
)
}
val stroke = helper.createButton().apply {
setText("Stroke")
setWidth(RemoteView.MATCH_PARENT); setHeight(32); setTextSize(14f)
setTextColor(RemoteViewColors.BLUE_PRIMARY)
setBackgroundShape(
ShapeDescriptor.Builder().stroke(1, RemoteViewColors.BLUE_PRIMARY).cornerRadius(4f).build()
)
}
val text = helper.createButton().apply {
setText("Text")
setWidth(RemoteView.MATCH_PARENT); setHeight(32); setTextSize(14f)
setTextColor(RemoteViewColors.BLUE_PRIMARY); setFontStyle(FontStyleType.BOLD)
}
// Combined buttons (TabLayout): outer-rounded/inner-square + weight even split
val leftCorner = floatArrayOf(4f, 4f, 0f, 0f, 0f, 0f, 4f, 4f)
val rightCorner = floatArrayOf(0f, 0f, 4f, 4f, 4f, 4f, 0f, 0f)
val tab = helper.createLinearLayoutHorizontal().apply {
setWidth(RemoteView.MATCH_PARENT); setHeight(RemoteView.WRAP_CONTENT)
}
val tabL = helper.createButton().apply {
setText("Primary"); setWidth(0); setHeight(32)
setTextColor(RemoteViewColors.WHITE); setFontStyle(FontStyleType.BOLD)
setBackgroundShape(ShapeDescriptor.Builder().fillColor(RemoteViewColors.BLUE_PRIMARY).cornerRadii(leftCorner).build())
}
val tabR = helper.createButton().apply {
setText("Secondary"); setWidth(0); setHeight(32)
setBackgroundShape(ShapeDescriptor.Builder().fillColor(RemoteViewColors.NEAR_WHITE_20).cornerRadii(rightCorner).build())
}
tab.addToRemote(tabL, WidgetLayoutParam().apply { weight = 1f })
tab.addToRemote(tabR, WidgetLayoutParam().apply { weight = 1f; marginLeft = 1 })
02 Component States (disabled)
// Create buttons
val disabledFilled = helper.createButton().apply {
setText("Filled"); setWidth(0); setHeight(32)
setTextColor(RemoteViewColors.WHITE); setFontStyle(FontStyleType.BOLD)
setBackgroundShape(ShapeDescriptor.Builder().fillColor(RemoteViewColors.BLUE_PRIMARY).cornerRadius(4f).build())
}
val disabledStroke = helper.createButton().apply {
setText("Stroke"); setWidth(0); setHeight(32); setTextColor(RemoteViewColors.BLUE_PRIMARY)
setBackgroundShape(ShapeDescriptor.Builder().stroke(1, RemoteViewColors.BLUE_PRIMARY).cornerRadius(4f).build())
}
val disabledText = helper.createButton().apply {
setText("Text"); setWidth(0); setHeight(32)
setTextColor(RemoteViewColors.BLUE_PRIMARY); setFontStyle(FontStyleType.BOLD)
}
// Disabled: no click + reduced opacity
listOf(disabledFilled, disabledStroke, disabledText).forEach { btn ->
btn.setEnabled(false) // does not respond to clicks
btn.setAlpha(0.4f) // visually conveys disabled
}
03 Component Styles (size / shape / custom colors)
// Sizes: L 32 / M 26 / S 20 (dp)
listOf(32, 26, 20).forEach { h ->
helper.createButton().apply {
setText("$h PX"); setWidth(0); setHeight(h)
setTextColor(RemoteViewColors.WHITE)
setBackgroundShape(ShapeDescriptor.Builder().fillColor(RemoteViewColors.BLUE_PRIMARY).cornerRadius(4f).build())
}
}
// Shapes: square(0) / rounded(4) / pill(24)
listOf(0f, 4f, 24f).forEach { radius ->
helper.createButton().apply {
setText("Shape"); setWidth(0); setHeight(32)
setTextColor(RemoteViewColors.WHITE)
setBackgroundShape(ShapeDescriptor.Builder().fillColor(RemoteViewColors.BLUE_PRIMARY).cornerRadius(radius).build())
}
}
// Custom colors: green fill / blue 2dp stroke / red text
val green = helper.createButton().apply {
setText("Filled"); setWidth(0); setHeight(32); setTextColor(RemoteViewColors.WHITE)
setBackgroundShape(ShapeDescriptor.Builder().fillColor(0xFF00C774.toInt()).cornerRadius(4f).build())
}
val blueStroke = helper.createButton().apply {
setText("Stroke"); setWidth(0); setHeight(32); setTextColor(RemoteViewColors.BLUE_PRIMARY)
setBackgroundShape(ShapeDescriptor.Builder().stroke(2, RemoteViewColors.BLUE_PRIMARY).cornerRadius(4f).build())
}
val redText = helper.createButton().apply {
setText("Text"); setWidth(0); setHeight(32)
setTextColor(0xFFFF4D4F.toInt()); setFontStyle(FontStyleType.BOLD)
}
For a full runnable example, see drawing/ButtonDrawer.kt in the official Sample
open-pilot-plugin-sample.