My button event is not firing

jkurant

Member
Licensed User
Longtime User
When the user presses the menu key during my activity, I want to display a settings screen, but I can't use the preferences library because it does not support color options.

So, I have a layout designed with the abstract designer that has a panel at the top with a label and a button. I want to do something when the button is clicked. The code is not running when the button is clicked, which I can tell because the msgbox in that code does not pop up.

Can anyone tell me what is wrong with my code?


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

   'I don't think I even need these variables, but just in case
   Dim ButtonFontSize As Button
   Dim ScrollView1 As ScrollView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Set up Activity
   StartAllOver
End Sub

Sub StartAllOver
   'Msgbox("Starting ALL OVER","StartAllOver")
   Activity.LoadLayout("MedsBarWithScrollView")
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
   If KeyCode = KeyCodes.KEYCODE_MENU Then
      DisplayPreferencesActivity
   End If
End Sub

Sub DisplayPreferencesActivity
   Dim MyPanel As Panel
   Dim MyLabel As Label
   Dim MyButton As Button
    Dim clrs(2) As Int
    clrs(0) = Colors.RGB(32, 32, 32)
    clrs(1) = Colors.RGB(64, 64, 64)
    Dim gdButton As GradientDrawable
    gdButton.Initialize("LEFT_RIGHT", clrs)
    gdButton.CornerRadius = 5
   
    clrs(0) = Colors.RGB(0, 0, 32)
    clrs(1) = Colors.RGB(0, 0, 64)
   Dim gdScrollView As GradientDrawable
   gdScrollView.Initialize("TOP_BOTTOM", clrs)
   
   Dim cdTransparent As ColorDrawable
   cdTransparent.Initialize(Colors.ARGB(0,0,0,0), 0)
   
   Activity.LoadLayout("PrefScroll")
   
   MyPanel.Initialize("")
   MyPanel.Background = cdTransparent
   ScrollView1.Panel.AddView(MyPanel, 0, 0, ScrollView1.Panel.Width, 70)
   ScrollView1.Background = gdScrollView

   MyLabel.Initialize("")
   MyLabel.Text = "Button font size"
   MyLabel.TextSize = preferences.GetString("ButtonFontSize")
   MyLabel.Background = cdTransparent
   MyLabel.Color = Colors.Transparent
   MyLabel.TextColor = Colors.White
   MyLabel.Typeface = Typeface.DEFAULT_BOLD
   MyPanel.AddView(MyLabel, 0, 5, ScrollView1.Panel.Width-30, 40)
   
   MyButton.Initialize("ButtonFontSize")
   MyButton.Text = ">"
   MyButton.TextColor = Colors.White
   MyButton.Typeface = Typeface.SANS_SERIF
   MyButton.TextSize = 18
   'MyButton.Background = cdTransparent
   MyButton.Background = gdButton
   'MyPanel.AddView(MyButton, ScrollView1.Panel.Width-30, 10, 30, 30)
   MyPanel.AddView(MyButton, 0, 10, 30, 30)
End Sub

' This code is never getting fired
Sub ButtonFontSize_Click
   Msgbox("ButtonFontSize_Click","ButtonFontSize_Click")
   CreatePreferenceScreen
   StartActivity(screen.CreateIntent)   
   'Activity.LoadLayout("Preferences")
End Sub

This code loads the first activity and when the user presses Menu, it displays the panel with the label and the button. However, clicking the button does nothing.

Doesn't MyButton.Initialize("ButtonFontSize") make Sub ButtonFontSize_Click the event handler? I don't understand why it is not firing.
 

klaus

Expert
Licensed User
Longtime User
What do you have in the PrefScroll layout file ?
If MyButton is already in the layout file then initializing it once more initiates a new instance of the button and nothing happens.

You must add Return True in the routine below.
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
    If KeyCode = KeyCodes.KEYCODE_MENU Then
        DisplayPreferencesActivity
        Return True
    End If
End Sub
It would be easier to give you a concrete answer if you posted your whole project source code as a zip file. Otherwise we must guess what you have done and where.

Best regards.
 
Upvote 0

jkurant

Member
Licensed User
Longtime User
Okay, I have posted all of the code. I did take out some code in what I had posted, but I doubt it matters. But I should let you guys decide that!

MyButton is just a variable I used to create a button and add it to the panel. I want to capture events from the various buttons I am going to create and I just can't get that to happen. And no, the layout does not contain a MyButton. Unless you think it does, somehow, after looking at my code.

I added the Return True my Activity_KeyPress, but that didn't fix the problem.
 

Attachments

  • KurantMeds.zip
    10.7 KB · Views: 178
Upvote 0

klaus

Expert
Licensed User
Longtime User
I just had a look at your code.
In the PrefScroll layout file you have on top a Panel that hides the button and label you add by code.
If you move down the Top of the ScrollView below Panel1, you'll see the views you add in the code. And the ButtonFontSize_Click Event is fired.
What is the purpose of the content of Panel1 ?

Without the whole code it would have been impossible to help you, that's the reason why we always ask for it.

Best regards.
 
Upvote 0

jkurant

Member
Licensed User
Longtime User
Problem solved!

Klaus,

Your suggestions of adding Return True to the event handler and removing the panel worked! Plus, my button was so tiny I could hardly touch it in the right stop.

I do have another related question, though. Please let me know if you want me to post this as a new thread.

In the designer I cannot make a panel a child of a ScrollView, which I don't understand. But I am trying in DisplayPreferencesActivity to add the label and button to the panel and then add the panel to the ScrollView.

Is there a reason I cannot make a panel a child of a ScrollView in the designer?

Thank you
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Is there a reason I cannot make a panel a child of a ScrollView in the designer?
There is no reason, because you can do it!
In the Desinger define a Panel with all the views you want or need, it can be bigger than the screen. Then you can load the layout file with ScrollView1.Panel.LoadLayout(...) and set the ScrollView1.Panel.Height according to the real Panel height.
You could have a look at this example: ScrollView example with a Panel higher than the screen.

Best regards.
 
Upvote 0

jkurant

Member
Licensed User
Longtime User
Adding items to a ScrollView in the designer

I couldn't do it in the designer, though. I can add a ScrollView and I can add a panel but I can't assign the panel's parent to be the ScrollView. I just tried it again to be sure.

However, I will try ScrollView.Panel.LoadLayout to load a layout I built in the designer into my ScrollView at runtime.

Thank you
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You have one layout file with the ScrollView, and you should define a separate layout file with a Panel and in this Panel you add all the views you need.
Then in the code you load this layout file with ScrollView1.Panel.LoadLayout(...).

Best regards.
 
Upvote 0

jkurant

Member
Licensed User
Longtime User
Klaus,

I have done exactly as you said. I have my layout PrefScroll which contains just a scrollview. Then I have another layout Preferences that has one long (tall) panel with a lot of other controls on it.

I load the PrefScroll layout onto the current Activity. Then I set the size of the ScrollView to the size of the Activity. Then I load my layout onto the panel in the ScrollView, which is taller than the screen height. But I am not getting any scrolling. There are six sets of buttons on my layout, but only four show up.

B4X:
Sub DisplayPreferencesActivity
   Activity.LoadLayout("PrefScroll")
   ScrollView1.Width = Activity.Width
   ScrollView1.Height = Activity.Height
   ScrollView1.Panel.LoadLayout("Preferences")
   Msgbox("ScrollView1.Panel.Height=" & ScrollView1.Panel.Height & "; Activity.Height=" & Activity.Height, "what gives?")
End Sub

I am attaching my code again.

Oh, and please let me know if this should be a new thread and I will create the new thread.

Thank you.
 

Attachments

  • KurantMeds2.zip
    11.5 KB · Views: 180
Upvote 0

jkurant

Member
Licensed User
Longtime User
LoadLayout seems to have set the size of the ScrollView.Panel to the same size as the Activity, because I had to reset the height with ScrollView1.Panel.Height = 900dip

B4X:
Sub DisplayPreferencesActivity
   Activity.LoadLayout("PrefScroll")
   ScrollView1.Width = Activity.Width
   ScrollView1.Height = Activity.Height
   ScrollView1.Panel.LoadLayout("Preferences")
   ScrollView1.Panel.Height = 900dip
End Sub
 
Upvote 0
Top