Android Question Hiding and Displaying Controls

Frank Cazabon

Member
Licensed User
Longtime User
I am trying to hide a button and display a label in its place depending on whether a record exists in a SQLite table.

My Activity has the button and label added to it in the Designer.

This is my code:

B4X:
Sub Globals
    Dim btnGetRunSheet As Button
    Dim lblSelectedRoute As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
    If FirstTime Then
        If File.Exists(SQLDataBasePath, SQLDateBaseName) = False Then
            'if not, initialize it
            SQLLite.Initialize(SQLDataBasePath, SQLDateBaseName, True)
            'and create it
            CreateDataBase
        Else
            'if yes, initialize it
            SQLLite.Initialize(SQLDataBasePath, SQLDateBaseName, True)
        End If
    End If
   
    HideGetRunSheet
   
    Activity.LoadLayout("MainMenu")
   
End Sub

Sub Activity_Resume
    HideGetRunSheet
End Sub

Sub HideGetRunSheet
    Dim Query As String
    Dim curSelectedRoute As Cursor
    Query = "SELECT srt_name FROM SelectedRoute"
    curSelectedRoute = SQLLite.ExecQuery(Query)
    curSelectedRoute.Position = 0

    btnGetRunSheet.Initialize("btnGetRunSheet")
    lblSelectedRoute.Initialize("lblSelectedRoute")
   
    If curSelectedRoute.RowCount > 0 Then
        btnGetRunSheet.Visible = False
        lblSelectedRoute.Text = curSelectedRoute.GetString("srt_name")
        lblSelectedRoute.Visible = True
    Else
        btnGetRunSheet.Visible = True
        lblSelectedRoute.Visible = False
    End If
End Sub

This seems to have no effect on the button or the label.

Any ideas where I am going wrong?
 

stevel05

Expert
Licensed User
Longtime User
You shouldn't Initialize the button and label in your code if they are added to the designer.
 
Upvote 0

Frank Cazabon

Member
Licensed User
Longtime User
So if I add them in the designer, how do I display/hide them in code? Or is it that if I want to do this I should not add them in the designer and do everything in code?
 
Upvote 0

marab

Member
Licensed User
Longtime User
and if you want to hide a group of the controls you can add them into a panel
and then hide the panel by setting the visible to false
 
Upvote 0

Frank Cazabon

Member
Licensed User
Longtime User
Hmm... That's what I tried first and got an error about having to initialize the button first. Is there an example of how to do this anywhere around here?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Have you named the button 'btnGetRunSheet' in the designer?
 
Upvote 0

Frank Cazabon

Member
Licensed User
Longtime User
OK, thanks. I've finally got it.

My problems were

1. I was trying to hide the buttons before I had loaded the layout.

2. I had used "Dim btnGetRunSheet As Button" where as the Generation uses "Private btnGetRunSheet As Button" (not sure if that really was a problem though).
 
Upvote 0
Top