View by code problem

namjeon

New Member
Licensed User
Longtime User
I added views by code (not using Designer & Abstract Designer window) and wrote subroutines for each view actions. But I cannot trigger any action at all except Activity_Create subroutine. I have Button, ToggleButton, SeekBar, Timer, MediaPlayer etc. in my app. When I created these views in Designer window, it compiled and run all right. I am trying to position views in the Activity window by relative coordinates to avoid creating different set layout variants for different device screen size.
Please help.
Nam
 
Last edited:

lagore

Active Member
Licensed User
Longtime User
have you created the click event for the button etc and declared the view in 'Globals' not 'Process_Globals'. to be sure you declare the event correctly use the shortcut for declaring subs eg type Sub then space the Tab, then a selection of events will show just select the correct one and fill in the objects name. (see the IDE tips)
 
Upvote 0

namjeon

New Member
Licensed User
Longtime User
Here is code.
MediaPlayer app.
Three panels for three windows.
Three menu button at the main window.
Menu changing to different window works all right,

In the first panel there is ToggleButton to play or pause MediaPlayer.
Seekbar to show progress.
Label to show time.

Problem:
I cannot make ToggleButton to work.

Thanks,
Nam
------------------------------

Sub Process_Globals
Dim MediaPlayer1 As MediaPlayer
Dim timer1 As Timer

End Sub

Sub Globals
Dim pnlPage1, pnlPage2, pnlPage3 As Panel
Dim pnlToolbox As Panel
Dim SeekBar2 As SeekBar
Dim ToggleButton1 As ToggleButton
Dim Label3 As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
Dim PanelHeight As Float
If FirstTime Then
MediaPlayer1.Initialize()
MediaPlayer1.Load(File.DirAssets, "NOW_Meditation_1.mp3")
timer1.Initialize("timer1", 1000)

End If
Activity.LoadLayout("Main")
pnlToolbox.Top = Activity.Height - pnlToolbox.Height
PanelHeight = pnlToolbox.Top - 5dip

pnlPage1.Initialize("")
Activity.LoadLayout("Page1")
pnlPage1.AddView(pnlPage1,0,0,100%x,PanelHeight)
pnlPage1.Visible=True

'music progress bar
SeekBar2.Initialize("")
Activity.AddView(SeekBar2,5%x,70%y,220dip,17dip)
'SeekBar2.Max = 35
SeekBar2.Max = MediaPlayer1.Duration
SeekBar2.Value = 0

ToggleButton1.Initialize("")
Activity.AddView(ToggleButton1,76%x,65%y,60dip,40dip)
ToggleButton1.Textoff = "pause"

Label3.Initialize("")
Activity.AddView(Label3,4%x,66%y,130dip,40dip)
Label3.Text = "P:"
Label3.TextColor = Colors.black

pnlPage2.Initialize("")
pnlPage2.LoadLayout("Page2")
Activity.AddView(pnlPage2,0,0,100%x,PanelHeight)
pnlPage2.Visible=False

pnlPage3.Initialize("")
pnlPage3.LoadLayout("Page3")
Activity.AddView(pnlPage3,0,0,100%x,PanelHeight)
pnlPage3.Visible=False
End Sub

Sub btnPage_Click
Dim Send As Button
Send = Sender

pnlPage1.Visible=False
pnlPage2.Visible=False
pnlPage3.Visible=False

Select Send.Tag
Case "1"
pnlPage1.Visible=True
Case "2"
pnlPage2.Visible=True
Case "3"
pnlPage3.Visible=True
End Select
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub ToggleButton1_CheckedChange(Checked As Boolean)
'play or pause
If MediaPlayer1.IsPlaying Then
MediaPlayer1.Pause
timer1.Enabled = False
Else
MediaPlayer1.Play
timer1.Enabled = True
timer1_Tick
End If
End Sub
Sub SeekBar2_ValueChanged (Value As Int, UserChanged As Boolean)
If UserChanged = False Then Return
MediaPlayer1.Position = Value / 100 * MediaPlayer1.Duration
If MediaPlayer1.IsPlaying = False Then
MediaPlayer1.Play
If ToggleButton1.Checked = False Then
ToggleButton1.Checked = True
Else
End If

End If
timer1.Enabled = True
timer1_Tick
End Sub
Sub timer1_Tick
If MediaPlayer1.IsPlaying Then
SeekBar2.Value = MediaPlayer1.Position / MediaPlayer1.Duration * 100
Label3.Text = "Progress: " & ConvertToTimeFormat(MediaPlayer1.Position) & _
" (" & ConvertToTimeFormat(MediaPlayer1.Duration) & ")"
End If
End Sub
Sub ConvertToTimeFormat(ms As Int) As String
'converts milliseconds to m:ss format.
Dim seconds, minutes As Int
seconds = Round(ms / 1000)
minutes = Floor(seconds / 60)
seconds = seconds Mod 60
Return NumberFormat(minutes, 1, 0) & ":" & NumberFormat(seconds, 2, 0) 'ex: 3:05
End Sub
 
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Needs Code tags to be easier to read, but it looks like you are initializing your controls with no Event Prefix. You give them a name in Initialize like ToggleButton1.Initialize("MyToggle") then your sub for an event would be sub MyToggle_CheckedChange(Checked As Boolean). Looks like you are calling your event subs with a prefix equal to the variable name which is fine, just use that in initialize like- ToggleButton1.Initialize("ToggleButton1")

It doesn't appear you are doing all by code too with LoadLayout being used. Watch initializing views part of your designed layouts since LoadLayout already calls initialize with the Event Name you set in the Designer. If you didn't actually design a layout called Page1, Page2, or Page3 then you don't need the LoadLayout commands used.
 
Last edited:
Upvote 0

namjeon

New Member
Licensed User
Longtime User
View by code

Problem was in initialization.
I followed your suggestion and it worked.
Thank you for your kind help.
Nam
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…