Android Code Snippet [B4X] B4XDialog with adjustable button widths

1762070032815.png


Cross platform code to adjust the button widths based on their text.

B4X:
Private Sub ArrangeButtons(Dialog1 As B4XDialog, Canvas1 As B4XCanvas)
    Dim offset As Int = Dialog1.Base.Width - 2dip
    Dim gap As Int = 4dip
    For Each BtnType As Int In Array(xui.DialogResponse_Cancel, xui.DialogResponse_Negative, xui.DialogResponse_Positive)
        For Each btn As B4XView In Dialog1.Base.GetAllViewsRecursive
            If Initialized(btn.Tag) And btn.Tag = BtnType Then 'ignore
                Dim r As B4XRect = Canvas1.MeasureText(btn.Text, btn.Font)
                Dim width As Float = r.Width + 30dip 'change padding as needed
                btn.SetLayoutAnimated(0, offset - width - gap, btn.Top, width, btn.Height)
                offset = offset - width - gap
            End If
        Next
    Next
End Sub

Usage:
B4X:
Private Sub Button1_Click
    Dim rs As Object = Dialog.Show("Testing", "abcde abc", "fg gf", "hk")
    ArrangeButtons(Dialog, cvs) 'cvs is a global B4XCanvas
    Wait For (rs) Complete (Result As Int)
End Sub

Example is attached.
 

Attachments

  • Measure.zip
    180.7 KB · Views: 10
Top