Android Question Declare two Views (Label, RadioButton) in the Sub module

KIM jihoon

Member
Licensed User
Longtime User
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim Label1 As Label=TestView(1)
    Dim Radio1 As RadioButton=TestView(2)
End Sub

Sub TestView (no) As View
    Select Case no
    Case 1:     Dim atView As Label
    Case 2:     Dim atView As RadioButton
    End Select

    atView.Initialize("")
    atView.Text="good"
    Return atView
End Sub

I want to return View (Label, RadioButton) selectively among 1 and 2.
Running the code gives the following error

can someone help

Error description: Current declaration does not match previous one.
Previous: {Type=Label,Rank=0, RemoteObject=True}
Current: {Type=RadioButton,Rank=0, RemoteObject=True}
Error occurred on line: 9
Case 2: Dim atView As RadioButton
 
Last edited:
I don't have B4A in front of me to test, but my first try would be:

B4X:
Sub TestView (ViewTypeNumber) As Object    'As View might work too
    
    Select ViewTypeNumber
        Case 1:
            Dim NewLabel as Label
            NewLabel.Initialize("")
            NewLabel.Text = "good"
            Return NewLabel

        Case 2:
            Dim NewRadioButton as RadioButton
            NewRadioButton.Initialize("")
            NewRadioButton.Text = "good"
            Return NewRadioButton

        Case Else:
            Log("Invalid ViewTypeNumber " & ViewTypeNumber)
            Log("Probably going to return Null")
            
    End Select
    
End Sub
 
Upvote 0
My second try would be:

B4X:
Sub TestView (ViewTypeNumber) As Object    'As View might work too

    Dim V as View    'default View to return if not ViewTypeNumber 1 or 2
 
    Select ViewTypeNumber
        Case 1:
            Dim NewLabel as Label
            V = NewLabel

        Case 2:
            Dim NewRadioButton as RadioButton
            V = NewRadioButton

    End Select

    V.Initialize("")
    V.Text = "good"

    Return V

End Sub

although I'm uncertain as to how a generic View is going to handle the .Initialize and .Text =
 
Upvote 0
thank you emexes

The purpose is to set common properties to Views (Label, RadioButton)
For example, Color, Typeface, Gravity, DEFAULT_BOLD, Left, Top, Width, Height, etc. (15 types)

This method also throws an error at v.Initialize("")
Error description: Unknown member: initialize
 
Upvote 0
Try this:

B4X:
Sub TestView (ViewTypeNumber) As Object    'As View might work too
    Dim v as View    'default View to return if not ViewTypeNumber 1 or 2
 
    Select ViewTypeNumber
        Case 1
            Dim NewLabel as Label = v
        Case 2
            Dim NewRadioButton as RadioButton = v
    End Select

    v.Text = "good"

    Return v
End Sub
 
Upvote 0
Try this:

B4X:
Sub TestView (ViewTypeNumber) As Object    'As View might work too
    Dim v as View    'default View to return if not ViewTypeNumber 1 or 2
 
    Select ViewTypeNumber
        Case 1
            Dim NewLabel as Label = v
        Case 2
            Dim NewRadioButton as RadioButton = v
    End Select

    v.Text = "good"

    Return v
End Sub
The test results in the following error

Error description: Unknown member: text
Error occurred on line: 11
v.Text = "good"
 
Upvote 0
B4X:
Sub TestView (ViewTypeNumber) As Object    'As View might work too
    Dim v As View    'default View to return if not ViewTypeNumber 1 or 2
    Select ViewTypeNumber
        Case 1
            Dim NewLabel As Label = v
            NewLabel.Text="good"
           ************************
           ************************
        Case 2
            Dim NewRadioButton As RadioButton = v
            NewRadioButton.Text="good"
           ************************
           ************************
    End Select
End Sub

Messy code, but I did it like this:
15 identical properties were put into each CASE

What better way than this ?
 
Upvote 0
The B4XView was specifically designed to allow common properties to be set for different views in a consistent way across platforms.

B4X:
    Dim lbl As Label
    lbl.Initialize("")
    Dim btn As Button
    btn.Initialize("")
    Dim views() As B4XView = Array As B4XView(lbl, btn)
    For Each vx As B4XView In views
        vx.Text = "XXXX"
    Next
    Log(lbl.Text & TAB & btn.Text)

'OR

    lbl.As(B4XView).Text = "AAAA"
    btn.As(B4XView).Text = "BBBB"
    Log(lbl.Text & TAB & btn.Text)
 
Upvote 0
thank you !
I got the following error

B4X:
    For Each vx As B4XView In Views
        vx.Text = "XXXX" '<- OK
        vx.Gravity=Gravity.CENTER '<- Error
        vx.Typeface=Typeface.DEFAULT_BOLD '<- Error
    Next
 
Upvote 0
The method names for B4XView are standard across views.
Just use vx and then period, the popup shows you all the relevant names of methods.

Font replaces Typeface, it takes a B4XFont as argument.
Centering of text is done with SetTextAlignment
 
Upvote 0
Back
Top