Android Question activity button beginners problem

OdyMiles

Member
Licensed User
Hi,
I don't get it - am I to stupid?

Here is my minimalist code and nothing is displayed at all...
No buttons... and: is "btn1_click" correct?

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
   Dim appactive As Boolean 
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    Dim btn1,btn2 As Button

    btn1.Initialize("btn1")
    btn2.Initialize("btn2")
    Activity.AddView(btn1, 10dip, 10dip, 100dip, 100dip)
    Activity.AddView(btn2, 110dip, 110dip, 100dip, 100dip)
    btn1.Text="1"
    btn2.Text="2"

    appactive = True

    Do While appactive = True
        '...   (1)see below
        Log("...")   ' log works fine
    Loop   
End Sub

Sub btn1_Click
    'Msgbox("bye","")
    appactive = False
End Sub


(1) if I put
Msgbox("wait","wait") the buttons are drawn but not accesible.


Thanks for helping a lost former c++ programmer...
Chris
 

Myr0n

Active Member
Licensed User
Longtime User
I suggest you to read the Begginers Guide that you can find here and read from page 33-34.

in your Activity_Create you should not have a do while because always gonna be true always will be stick in there, if you comment that do while should work.
 
Upvote 0

OdyMiles

Member
Licensed User
Thank you - I know that example in the guide...

Even with the code in separate sub it doesn't work
I just want to display a button and idle cyle in my app until the button is pressed...
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
placing a do while in activity create is not going to work...

Try this (direct edit of you posted code, not tried in ide)

B4X:
Sub Globals
   Dim Cycler as timer
End Sub

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
   Dim appactive As Boolean

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    Dim btn1,btn2 As Button

    btn1.Initialize("btn1")
    btn2.Initialize("btn2")
    Activity.AddView(btn1, 10dip, 10dip, 100dip, 100dip)
    Activity.AddView(btn2, 110dip, 110dip, 100dip, 100dip)
    btn1.Text="1"
    btn2.Text="2"

    appactive = True

    Cycler.Initialize("cycler", 200)
    Cycler.Enabled = True 
End Sub

Sub btn1_Click
    'Msgbox("bye","")
    appactive = False
End Sub

Sub Cycler_Tick
   If appactive then
   Log("...")   ' log works fine
   DoEvents  
  end if
 
Upvote 0

Myr0n

Active Member
Licensed User
Longtime User
Please, comment the do while, I tested and the button 1 works fine.
 
Upvote 0

Myr0n

Active Member
Licensed User
Longtime User
And uncomment your Msgbox in your click event.
 
Upvote 0

Myr0n

Active Member
Licensed User
Longtime User
@Myron: ok, when I comment the do while my buttons are displayed but the button does not react at all.
@Cableguy: thanks I will try this in my IDE and get back to you

Doesn't react because it doesn't have anything to show you.

B4X:
Sub btn1_Click
    Msgbox("bye","")
    appactive = False
End Sub
 
Last edited:
Upvote 0

OdyMiles

Member
Licensed User
Thats it Cableguy, great !!! Thanks, now my eyample works fine :)

B4X:
sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim appactive As Boolean
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    test1
End Sub

Sub test1
    Private btn1,btn2 As Button
    Dim Cycler As Timer

    btn1.Initialize("btn1")
    btn2.Initialize("btn2")
    Activity.AddView(btn1, 10dip, 10dip, 100dip, 100dip)
    Activity.AddView(btn2, 110dip, 110dip, 100dip, 100dip)
    btn1.Text="1"
    btn2.Text="2"

    appactive = True
    Cycler.Initialize("cycler", 200)
    Cycler.Enabled = True
End Sub

Sub Cycler_Tick
   If appactive Then
   Log("...")   ' log works fine
   DoEvents
   Else
       ExitApplication
  End If
End Sub

Sub btn1_Click
    appactive = False
    Msgbox("Exit App","")

and of course: thanks to Myron...

Chris


Now I am happy :)
 
Last edited:
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Do read the beginners guide about working with timers.
They should be declared under globals, and paused in activity pause... Or risk a memory leak
 
Upvote 0
Top