Button

2026-09-20Loading rating…

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)

MethodParamsDescription
setWidthIntWidth (dp); special values RemoteView.MATCH_PARENT(-1) / WRAP_CONTENT(-2)
setHeightIntHeight (dp); same special values
setMinSizeminWidth: Int, minHeight: IntMinimum size (dp)
setPaddingleft, top, right, bottom: IntPadding (dp)
setVisibilityVisibilityTypeVisibility: VISIBLE / INVISIBLE / GONE
setAlphaFloatOpacity, 0.0 – 1.0
setEnabledBooleanWhether enabled; disabled does not respond to clicks

Background & Shape (inherited from RemoteViewProvider)

MethodParamsDescription
setBackgroundIntSolid background color (ARGB Int)
setBackgroundShapeShapeDescriptorBackground shape: fill / stroke / corner radius, see ShapeDescriptor below

ShapeDescriptor.Builder composable items:

Builder methodParamsDescription
fillColorIntFill color (ARGB)
strokewidthDp: Int, color: IntStroke width and color
cornerRadiusdp: FloatUniform corner radius
cornerRadiiFloatArray(8)Independent corners (top-left xy, top-right xy, bottom-right xy, bottom-left xy)
shapeSHAPE_RECTANGLE / SHAPE_OVALRectangle or oval

Text Properties

MethodParamsDescription
setTextStringButton text
setTextSizeFloatText size (rounded to sp internally)
setTextColorIntText color (ARGB Int)
setFontStyleFontStyleTypeFont style: NORMAL / BOLD / ITALIC / BOLD_ITALIC
setGravityGravityTypeAlignment: CENTER / START / END / LEFT / RIGHT / TOP / BOTTOM, etc.
setMaxLinesIntMax number of lines
setSingleLineBooleanWhether single line
setEllipsizeEllipsizeTypeEllipsis position: START / MIDDLE / END / MARQUEE
setTextDecorationTextDecorationTypeDecoration: NONE / UNDERLINE / STRIKE_THROUGH / UNDERLINE_AND_STRIKE_THROUGH
setLetterSpacingFloatLetter spacing
setLineSpacingextra: Float, multiplier: FloatLine spacing (extra + multiplier)

Events

MethodDescription
setOnClickListenerClick callback. Runs on the Binder thread pool — switch to the main thread yourself for UI/business

3. Tips

  1. Colors must be ARGB Int. setTextColor / setBackground accept an Int color value (e.g. 0xFF2D8CF0.toInt() or Color.RED); the underlying updateView converts it to a decimal string. Do not pass "#RRGGBB", "0xAARRGGBB", or R.color.xxx.
  2. 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 with Handler(Looper.getMainLooper()).post { ... }.
  3. Call syncToRemote() after changing a mounted component's properties. setXxx only updates the local cache; syncToRemote() batch-syncs to Pilot. If you configure properties before mounting, no extra call is needed.
  4. Dim the disabled state manually. setEnabled(false) only stops responding to clicks; visually, also call setAlpha(0.6f) to convey the disabled look.
  5. Stroke button = no fill. Call only stroke(...) without fillColor to get a hollow stroke effect; for a text button, do not call setBackgroundShape.
  6. Setting width to 0 with a parent container weight achieves 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.ktopen in new window in the official Sample open-pilot-plugin-sample.

Last Updated: