Making a scrollview with buttons

DGChainZ

Member
So I am currently working on my first app. It is a basic soundboard with simple buttons which play .wav files. I have the buttons working, I just need more room to add more buttons. I want to create a vertical scrollview but am having no luck.

All I want to do is have the app scroll in order to display additional buttons to click on. I'm not sure how to embed the buttons into a scrollview.

Any help is appreciated.
 

mjcoon

Well-Known Member
Licensed User
Any help is appreciated.

Erel reads this part of the forum: Android Development Forum - Basic4android > Basic4ppc (Windows Mobile) (see top of screen).

But people only interested in Android may not.

Mike.
 

DGChainZ

Member
I have read all of the scrollview examples and none of them seem to apply directly to my issue. I have tried to adopt them, but am still having problems. For example, the scrollview example you listed is loading bitmap images. I do not need to load images at all, simply scroll with buttons.
 

DGChainZ

Member
What have you tried !
If the example adds ImageViews replace these by Buttons.

Otherwise you could post your project as a zip file (IDE menu File / Export As Zip).

Best regards.

I have tried adding a scrollview (declaring it inside the SUB Globals), from there I am kinda lost.

Do I need to assign each button a number so that I can run a FOR loop? and if so how?

Also, it seems my zip is too large to upload for some reason, even though I have only 71 lines of code.
 

DGChainZ

Member
Since I cannot add my zip file, here is some of my code:

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim btnplay As Button
Dim mp As MediaPlayer
Dim SV As ScrollView
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("first")
mp.Initialize
SV.Initialize
SV.Panel.AddView(btnplay,10,190,130,60)
btnplay.(Not sure what to put here)
End Sub

One of my buttons looks like this:

Sub fin_Click
mp.Load(File.DirAssets,"Let_me_finish.wav")
mp.Play
End Sub
 

DGChainZ

Member
Do you have big images ?
Attached a very simple example.

Best regards.

The buttons I would be using are around the same size as the one in your example. It seems your example is nearly identical to what I need, except I would like to have two columns instead of one.

I also need each individual button to play a different .wav file. I'm not sure how to do this without naming each file a #.wav and running another FOR loop inside the button click sub.
 

DGChainZ

Member
I think I could make two columns simply by adding a couple IF statements inside the FOR loop you have made.

I'm still at a loss for how to access different .wav files with each button.
 

klaus

Expert
Licensed User
Longtime User
If you want a more precise answer you must ask precise questions with all the details you want or need.
Otherwise how do you expect us to guess what you want.
It's the first time you speek about two columns. The principle is the same you just need to change the Left and Top properties accordingly.
What are the names of your wav files ?
You could organize these files with a same generic name and an index so they could be run directly in the btnTest_Click routine knowing the Button index.
Give more details so we could give you a working example at once.

Best reagrds.
 

DGChainZ

Member
I was able to modify your Activity_Create subroutine to obtain two columns. I'm not sure if it is the most efficient way, but here is the new sub:

Sub Activity_Create(FirstTime As Boolean)
svcButtons.Initialize(100)
Activity.AddView(svcButtons, 0, 0, 100%x, 100%y)

Dim i As Int
btnSpace = btnHeight + 1dip
For i = 0 To 19
If i Mod 2=0 Then
btnTest(i).Initialize("btnTest")
svcButtons.Panel.AddView(btnTest(i), 10dip, 10dip + i * btnSpace, btnWidth, btnHeight)
btnTest(i).Text = "Test " & i
btnTest(i).Tag = i
Else
btnTest(i).Initialize("btnTest")
svcButtons.Panel.AddView(btnTest(i), 250, 10dip + (i-1) * btnSpace, btnWidth, btnHeight)
btnTest(i).Text = "Test " & i
btnTest(i).Tag = i
End If
Next
svcButtons.Panel.Height = 10dip + i * btnSpace
End Sub


My wav files are all random strings.wav. So a specific question would be, could I modify them to be say 1.wav, 2.wav, 3.wav etc....and access them with a line like this:

mp.Load(File.DirAssets,i & ".wav") ?

What does the syntax need to be on this line?
 

klaus

Expert
Licensed User
Longtime User
In your code you should put these two lines out of the If / Else / End If
B4X:
btnTest(i).Text = "Test " & i
btnTest(i).Tag = i
The height of the ScrollView.Panel must be shorten.

You could also do it like this:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    svcButtons.Initialize(100)
    Activity.AddView(svcButtons, 0, 0, 100%x, 100%y)

    Dim i, j As Int
    btnSpaceX = btnWidth + 10dip
    btnSpaceY = btnHeight + 1dip
    For i = 0 To 19
        btnTest(i).Initialize("btnTest")
        j = i / 2
        svcButtons.Panel.AddView(btnTest(i), 10dip + (i Mod 2) * btnSpaceX, 10dip + j * btnSpaceY, btnWidth, btnHeight)
        btnTest(i).Text = "Test " & i
        btnTest(i).Tag = i
    Next
    j = (i + 1) / 2
    svcButtons.Panel.Height = 10dip + j * btnSpaceY
End Sub
If you set the wav files from 0.wav 1.wav etc the btnTest routine could look like this:
B4X:
Sub btnTest_Click
    Dim btn As Button
    
    btn = Sender
    mp.Load(File.DirAssets, btn.Tag & ".wav")
    mp.Play
End Sub
Best regards.
 

DGChainZ

Member
I want my soundboard to look similar to the following:

ht tp://cdn6.staztic.com/cdn/ screenshot/nlappferroscarface-1-0.jpg

Some specific questions are as follows:

1. How do you index .wav files and then access them?

2. How do you create a list of strings that you could access with the same index in order to display different text on each button (as seen in the above picture)?

I would like to have one button_click sub routine in order to achieve both of the above tasks. I'm fairly confident the .wav file indexing can be done but am unaware of the specific "call" syntax. I'm not sure about a "database" of strings that could be accessed in a similar manner.:sign0104:
 

DGChainZ

Member
2. How do you create a list of strings that you could access with the same index in order to display different text on each button (as seen in the above picture)?

Sorry about the last post, you answered most of the questions in there. The above question is the only thing I have remaining. Thanks again for all of your help!
 

klaus

Expert
Licensed User
Longtime User
You can:
Dim two string arrays:
B4X:
Dim btnName(20) As String
Dim wavName(20) As String
Define an Init routine:
B4X:
Sub Init
    btnName(0) = "xxxx"
    btnName(1) = "xxxy"
    btnName(2) = "xxyy"
    .
    wavName(0) = "yyyy.wav"
    wavName(1) = "yyyz.wav"
    wavName(2) = "yyzz.wav"
    .
End Sub
When adding the Buttons to the ScrollView:
B4X:
btnTest(i).Text = btnName(i)
And the Click routine:
B4X:
Sub btnTest_Click
    Dim btn As Button
    
    btn = Sender
    mp.Load(File.DirAssets, wavName(btn.Tag))
    mp.Play
End Sub
Then it depends on what exactly you want to do. An application with fixed values or random values from a database.

Best regards.
 

DGChainZ

Member
Klaus, if you are still reading this. The code isn't working!

Everything is displaying correctly, except the text for each button isn't displaying. Also, when I click on the buttons I receive a java run time error and it pauses the program on that line.

Here is my complete code for your examination:

'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
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("bob.jpg")
svcButtons.Initialize(100)
'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

mp.Load(File.DirAssets, btn.Tag & ".wav")
mp.Play
End Sub
 
Top