B4X Store
B4A · XUI · UIButton

UIButton

Practical guide to building gradient and outlined buttons with icons, images, B4X properties, shadows and Click / LongClick events. Includes practical examples and the updated public API reference.

24
Methods
3
Fields
8
Designer properties
2
Events

Interactive preview

Tap or hold for one second to try the events. This web illustration does not execute B4A code.

Style 1 · gradient

STYLE_FILLED · GRADIENT_LEFT_RIGHT

Style 2 · outline

STYLE_OUTLINED · color

Last simulated event
Interact with a button

1. When to use UIButton

Primary actions

Save, submit, confirm, or navigate with a branded gradient button.

Secondary actions

Cancel, back, or edit with an outline button.

Buttons with icons

Combine FontAwesome glyphs and a label in one rich-text view.

Buttons with images

Embed a local logo via CSBuilder without an extra ImageView.

2. Practical project tutorials

From a login screen to an icon-based Save button: each case explains when it is useful, how to configure it and how to handle its events.

Tutorial 1

Solid button from code

When to use it: Solid button from code

Implementation steps

B4A · BASIC
Dim btn As UIButton
btn.Initialize(Me, "btn")
btn.AddToParent(Activity, 16dip, 24dip, 220dip, 52dip, _
    btn.STYLE_FILLED, 16, "Guardar", Colors.White, 0xFF2563EB, 12dip)

Private Sub btn_Click
    Log("Guardar")
End Sub
Tutorial 2

Gradient button from code

When to use it: Gradient button from code

Implementation steps

B4A · BASIC
Dim btn As UIButton
btn.Initialize(Me, "btn")
Dim bg As List
bg.Initialize
bg.Add(0xFF6750A4)
bg.Add(0xFF03DAC6)
btn.AddToParent(Activity, 16dip, 24dip, 220dip, 52dip, _
    btn.STYLE_FILLED, 16, "Continuar", Colors.White, bg, 12dip)
btn.GradientDirection = btn.GRADIENT_LEFT_RIGHT
Tutorial 3

Outlined button

When to use it: Outlined button

Implementation steps

B4A · BASIC
Dim btnCancel As UIButton
btnCancel.Initialize(Me, "Cancel")
btnCancel.AddToParent(Activity, 16dip, 90dip, 220dip, 52dip, _
    btnCancel.STYLE_OUTLINED, 15, "Cancelar", 0xFF6750A4, _
    0xFF6750A4, 12dip)

3. B4X properties: correct set/get usage

When both setX(Value) and getX are present, use X as a property. You do not have to write setText or getText in your application.

Direct example
UIButton1.Text = "Hola"
Dim dato As String = UIButton1.Text
Log(dato)

Text setText / getText

Assign and read this as a B4X property. The original setText/getText method signatures remain in the API listing.

BtnAction.Text = "Continuar"
Dim dato As String = BtnAction.Text

TextColor setTextColor / getTextColor

Assign and read this as a B4X property. The original setTextColor/getTextColor method signatures remain in the API listing.

BtnAction.TextColor = 0xFFFFFFFF
Dim dato As Int = BtnAction.TextColor

TextSize setTextSize / getTextSize

Assign and read this as a B4X property. The original setTextSize/getTextSize method signatures remain in the API listing.

BtnAction.TextSize = 18
Dim dato As Int = BtnAction.TextSize

CornerRadius setCornerRadius / getCornerRadius

Assign and read this as a B4X property. The original setCornerRadius/getCornerRadius method signatures remain in the API listing.

BtnAction.CornerRadius = 14dip
Dim dato As Int = BtnAction.CornerRadius

MainTextTypeface setMainTextTypeface / getMainTextTypeface

Assign and read this as a B4X property. The original setMainTextTypeface/getMainTextTypeface method signatures remain in the API listing.

BtnAction.MainTextTypeface = Typeface.DEFAULT_BOLD
Dim fuente As Typeface = BtnAction.MainTextTypeface

BackgroundColors

Reading works as a property. Setting both colors uses setBackgroundColors with TWO arguments; BackgroundColors = ... does not work like single-value properties.

Dim colores() As Int = BtnAction.BackgroundColors
BtnAction.setBackgroundColors(0xFF7C3AED, 0xFF0891B2)

4. All 11 AddToParent parameters

These arguments configure position, size, style, text, background and corner radius. Gradient direction is set separately.

ParameterExampleHow it works
Parent

B4XView

—B4XView
l

Int

—Int
t

Int

—Int
w

Int

—Int
h

Int

—Int
styleButtom

Int

—Int
text_zise

Float

—Float
text

String

—String
textcolor

Int

—Int
backgroundColorOrColors

Object

—Object
corner

Int

—Int

5. Designer properties (8)

Names match the original library exactly, including ButtomType and cornerradius.

KeyTypeDefaultPurpose
TextColorColor0xFFFFFFFF#DesignerProperty: Key: TextColor, DisplayName: Text Color, FieldType: Color, DefaultValue: 0xFFFFFFFF, Description: Text color
SelectcolorBooleanTrue#DesignerProperty: Key: Selectcolor, DisplayName: ColorEffectSelecting, FieldType: Boolean, DefaultValue: True, Description: Enable/disable color effect when selecting
ButtomTypeString1#DesignerProperty: Key: ButtomType, DisplayName: Style, FieldType: String, DefaultValue: 1, List: 1|2, Description: Button style (1 or 2)
MainTextStringClick me#DesignerProperty: Key: MainText, DisplayName: Select Text, FieldType: String, DefaultValue: Click me, Description: Text displayed on the button
TextSizeInt12#DesignerProperty: Key: TextSize, DisplayName: Text Size, FieldType: Int, DefaultValue: 12, MinRange: 8, MaxRange: 40, Description: Set the text size
cornerradiusInt10#DesignerProperty: Key: cornerradius, DisplayName: Corner Radius, FieldType: Int, DefaultValue: 10, MinRange: 0, MaxRange: 100, Description: Set the corner radius
BackgroundColorColor#FF77777B#DesignerProperty: Key: BackgroundColor, DisplayName: Background Color, FieldType: Color, DefaultValue: #FF77777B, Description: Solid background color; set a color list in code for an automatic gradient
GradientDirectionStringAUTO#DesignerProperty: Key: GradientDirection, DisplayName: Gradient Direction, FieldType: String, DefaultValue: AUTO, List: AUTO|TOP_BOTTOM|TR_BL|RIGHT_LEFT|BR_TL|BOTTOM_TOP|BL_TR|LEFT_RIGHT|TL_BR, Description: AUTO uses the mesh gradient; a direction uses a linear gradient

6. Events and public fields

Click

Event declared by UIButton.

LongClick

Event declared by UIButton.

FieldTypeMeaning
mBaseB4XViewReal button container; use after creation
TagObjectOriginal layout Tag preserved in the instance
ColorEffectSelectingBooleanEnables/disables press ripple; defaults to True

7. Reference of 24 methods

This reference lists UIButton public API signatures. Code samples are displayed only when provided by the catalog.

Short examples assume a B4A project referencing ElevateUI, with xui As XUI and BtnAction As UIButton declared in Sub Globals. Create BtnAction first with Initialize + AddToParent or load a Designer layout using Activity.LoadLayout. Snippets mentioning BtnCancel also assume that second button has been created. Single-argument set/get pairs use property syntax: BtnAction.Text = "Hello" and Dim value As String = BtnAction.Text.
Public Sub Initialize (Callback As Object, EventName As String)

Purpose: Prepare the instance before its other methods.

Public Sub DesignerCreateView (BASE As Object, Lbl As Label, Props As Map)

Purpose: Invoked automatically by the B4A Visual Designer; do not call manually.

Public Sub AddToParent(Parent As B4XView, l As Int, t As Int, w As Int, h As Int, styleButtom As Int, text_zise As Float, text As String, textcolor As Int, backgroundColorOrColors As Object, corner As Int)

Purpose: Public API declaration; refer to the library behavior for usage details.

Public Sub setIcon(icon As String, icon_color As Int, iconTypeface As Typeface)

Purpose: Update icon at runtime when this setting changes.

Public Sub SetImage(DirImage As String, FileImage As String)

Purpose: Use SetImage for its named operation; follow its exact signature.

Sub setText(text As String)

Purpose: Update text at runtime when this setting changes.

Sub getText As String

Purpose: Read the current text value for validation or UI synchronization.

Public Sub setTextColor(Color As Int)

Purpose: Update text color at runtime when this setting changes.

Public Sub getTextColor As Int

Purpose: Read the current text color value for validation or UI synchronization.

Public Sub setTextSize(Size As Int)

Purpose: Update text size at runtime when this setting changes.

Public Sub getTextSize As Int

Purpose: Read the current text size value for validation or UI synchronization.

Public Sub setCornerRadius(Radius As Int)

Purpose: Update corner radius at runtime when this setting changes.

Public Sub getCornerRadius As Int

Purpose: Read the current corner radius value for validation or UI synchronization.

Public Sub setBackgroundColor(Color As Int)

Purpose: Public API declaration; refer to the library behavior for usage details.

Public Sub setBackgroundColors(ColorsList As List)

Purpose: Public API declaration; refer to the library behavior for usage details.

Public Sub getBackgroundColors As Int()

Purpose: Read the current background colors value for validation or UI synchronization.

Public Sub setGradientDirection(Direction As String)

Purpose: Public API declaration; refer to the library behavior for usage details.

Public Sub getGradientDirection As String

Purpose: Public API declaration; refer to the library behavior for usage details.

Public Sub setMainTextTypeface(TextTypeface1 As Typeface)

Purpose: Update main text typeface at runtime when this setting changes.

Public Sub getMainTextTypeface As Typeface

Purpose: Read the current main text typeface value for validation or UI synchronization.

Public Sub Elevation(BtElevation As Float, color As Int)

Purpose: Use Elevation for its named operation; follow its exact signature.

Public Sub SetTextGlow(Radius As Float, GlowColor As Int)

Purpose: Use SetTextGlow for its named operation; follow its exact signature.

Public Sub setStyleCode(StyleCode As Int)

Purpose: Get/set runtime button style; 1 filled, 2 outlined.

Public Sub getStyleCode As Int

Purpose: Get/set runtime button style; 1 filled, 2 outlined.