Need help assigning strings to buttons

DGChainZ

Member
So my first program is a basic soundboard. I'm unable to upload a zip file as it is too large but I'll post the code below.

My scrollview was working until I added a subroutine called Sub Init. This routine assigned a string to an array of button names (called btnName(i)). I've been told to "call" this routine before anything else in my Activity_Create sub. I'm not sure exactly where I'm going wrong and could use your help!

Here is my code:
B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

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.
   Dim mp As MediaPlayer
   Dim svcButtons As ScrollView
   Dim btnTest(13) As Button
   Dim btnWidth = 150dip As Int
   Dim btnHeight = 60dip As Int
   Dim btnSpace As Int
   Dim ImageView1 As ImageView
   Dim btnName(13) As String
End Sub

'button text
Sub Init As String
   btnName(0) = "fun problem"
   btnName(1) = "stupid #"
   btnName(2) = "Jesus God"
   btnName(3) = "finish"
   btnName(4) = "aliens"
   btnName(5) = "wall"
   btnName(6) = "heart"
   btnName(7) = "little activity"
   btnName(8) = "hit that"
   btnName(9) = "idiot"
   btnName(10) = "mexicans"
   btnName(11) = "political bs"
   btnName(12) = "wacko"
End Sub


Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   mp.Initialize
   'Activity.LoadLayout("first")
   ImageView1.Initialize("")
   svcButtons.Initialize(100)
   ImageView1.Bitmap = LoadBitmap(File.DirAssets,"bob.jpg")
   Activity.AddView(ImageView1, 30dip, 10dip, 260dip, 170dip)
   Activity.AddView(svcButtons, 0, 0, 100%x, 100%y)
   
   Dim i As Int
   btnSpace = btnHeight / 2 + 1dip
   For i = 0 To 12
      If i Mod 2=0 Then 
      btnTest(i).Initialize("btnTest")
      svcButtons.Panel.AddView(btnTest(i), 10dip, 190dip + i * btnSpace, btnWidth, btnHeight)
      Else
      btnTest(i).Initialize("btnTest")
      svcButtons.Panel.AddView(btnTest(i), 160dip, 190dip + (i-1) * btnSpace, btnWidth, btnHeight)
      End If
      btnTest(i).Text = btnName(i) 
      btnTest(i).Tag = i
   Next
   svcButtons.Panel.Height = 10dip + i * btnSpace
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

'Sound Clips
Sub btnTest_Click
   Dim btn As Button
   
   btn = Sender
   mp.Load(File.DirAssets, btn.Tag & ".wav")
   mp.Play
End Sub
 

DGChainZ

Member
Ok guys, I have fixed several issues but I am currently filling my button text with "false" on all buttons.

I believe I am calling my array correctly, so I'm unsure why I'm receiving a "false" for my string.

B4X:
'Main Activity
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

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.
   Dim mp As MediaPlayer
   Dim svcButtons As ScrollView
   Dim btnTest(13) As Button
   Dim btnWidth = 150dip As Int
   Dim btnHeight = 60dip As Int
   Dim btnSpace As Int
   Dim ImageView1 As ImageView
   Dim btnName(13) As String

End Sub

'button text
Sub Init (x,j As Int) As String
   btnName(0) = "fun problem"
   btnName(1) = "stupid #"
   btnName(2) = "Jesus God"
   btnName(3) = "finish"
   btnName(4) = "aliens"
   btnName(5) = "wall"
   btnName(6) = "heart"
   btnName(7) = "little activity"
   btnName(8) = "hit that"
   btnName(9) = "idiot"
   btnName(10) = "mexicans"
   btnName(11) = "political bs"
   btnName(12) = "wacko"

   Return x = btnName(j)

End Sub


Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   mp.Initialize
   'Activity.LoadLayout("first")
   svcButtons.Initialize(100)
   Activity.AddView(svcButtons, 0, 0, 100%x, 100%y)
   
   ImageView1.Initialize("")
   ImageView1.Bitmap = LoadBitmap(File.DirAssets,"bob.jpg")
   svcButtons.Panel.AddView(ImageView1, 30dip, 10dip, 260dip, 170dip)
   
   btnSpace = btnHeight / 2 + 1dip
   
   Dim i As Int
   Dim x As String 
   For i = 0 To 12
      If i Mod 2=0 Then 
      btnTest(i).Initialize("btnTest")
      svcButtons.Panel.AddView(btnTest(i), 10dip, 190dip + i * btnSpace, btnWidth, btnHeight)
      Else
      btnTest(i).Initialize("btnTest")
      svcButtons.Panel.AddView(btnTest(i), 160dip, 190dip + (i-1) * btnSpace, btnWidth, btnHeight)
      End If
      btnTest(i).Text = Init(x,i)
      btnTest(i).Tag = i
   Next
   svcButtons.Panel.Height = 10dip + (2 * i) * btnSpace
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

'Sound Clips
Sub btnTest_Click
   Dim btn As Button
   
   btn = Sender
   mp.Load(File.DirAssets, btn.Tag & ".wav")
   mp.Play
End Sub

I really need some help! Thanks!:sign0104:
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Why are you setting parameters to your sub's title?
'sub init' should be enough.
and where are you setting the button().text commands?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I've been told to "call" this routine before anything else in my Activity_Create sub.
No, I told you to call it just before adding the Buttons.

Here you are:
B4X:
Sub Globals
    Dim mp As MediaPlayer
    Dim svcButtons As ScrollView
    Dim btnTest(13) As Button
    Dim btnWidth = 150dip As Int
    Dim btnHeight = 60dip As Int
    Dim btnSpace As Int
    Dim ImageView1 As ImageView
    Dim btnName(13) As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    mp.Initialize

    svcButtons.Initialize(100)
    Activity.AddView(svcButtons, 0, 0, 100%x, 100%y)
    
    ImageView1.Initialize("")
    ImageView1.Bitmap = LoadBitmap(File.DirAssets,"bob.jpg")
    svcButtons.Panel.AddView(ImageView1, 30dip, 10dip, 260dip, 170dip)
    
    btnSpace = btnHeight / 2 + 1dip

    Init
    
    Dim i As Int
    Dim x As String 
    For i = 0 To 12
        If i Mod 2=0 Then 
            btnTest(i).Initialize("btnTest")
            svcButtons.Panel.AddView(btnTest(i), 10dip, 190dip + i * btnSpace, btnWidth, btnHeight)
        Else
            btnTest(i).Initialize("btnTest")
            svcButtons.Panel.AddView(btnTest(i), 160dip, 190dip + (i-1) * btnSpace, btnWidth, btnHeight)
        End If
        btnTest(i).Text = btnName(i)
        btnTest(i).Tag = i
    Next
    svcButtons.Panel.Height = 10dip + (2 * i) * btnSpace
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

'Sound Clips
Sub btnTest_Click
    Dim btn As Button
    
    btn = Sender
    mp.Load(File.DirAssets, btn.Tag & ".wav")
    mp.Play
End Sub

Sub Init
    btnName(0) = "fun problem"
    btnName(1) = "stupid #"
    btnName(2) = "Jesus God"
    btnName(3) = "finish"
    btnName(4) = "aliens"
    btnName(5) = "wall"
    btnName(6) = "heart"
    btnName(7) = "little activity"
    btnName(8) = "hit that"
    btnName(9) = "idiot"
    btnName(10) = "mexicans"
    btnName(11) = "political bs"
    btnName(12) = "wacko"
End Sub
Best regards.
 
Last edited:
Upvote 0

DGChainZ

Member
Thanks, Klaus.

I was unaware that you could call a sub without passing it parameters and also not declaring a "Return" value in the sub. Interesting!

Sorry if my questions seem odd, I come from a background in C and this VB thing is a work in progress.

One quick concept question. What is the priority for the Visual designer vs. code? Say I wanted to use the visual designer to do most of the layout, but I want to use code to make the scrollview with buttons. Is there a way to do both?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Say I wanted to use the visual designer to do most of the layout, but I want to use code to make the scrollview with buttons. Is there a way to do both?
Do both where ? In the Designer or in the code ?
In the code you can do whatever you want but without seeing where you put the different views before compiling.
For the ScrollView you can define it in the Designer in one layout file but you need to define another layout file with the content of the internal panel of the ScrollView but you must load this layout in the code with ScrollView.Panel.LoadLayout(filename) .

Did you have a look at the Beginner's Guide ?

Best regards.
 
Upvote 0

DGChainZ

Member
Did you have a look at the Beginner's Guide ?

Yes, I have had a look at the guide. I think I have resolved that particular issue.

Is there a way to brighten a section of the screen based on a certain condition? I would like to make a transparent button, where when pressed the pixels in the background image brighten.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I would like to make a transparent button, where when pressed the pixels in the background image brighten.
You could do this defining two bitmaps one semitransparent and the other transparent and in the Designer use StateListDrawable for the Drawable property.

You could have a look at the User's Guide chapter 9.1 View Drawables.

Best regards.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…