Android Question B4XPages - how to handle the same button events on different pages ?

Phayao

Active Member
Licensed User
Longtime User
Hello again,

another amateur question about pages:
I'm using the same customlayoutdialog on different pages. In this layout are a lot of buttons that serve the same purpose (concrete: to make a selected text in the SMMEditor bold, italic etc).
Now i have to define the same button_click events on all these pages - not a good programming practice !
But i cannot figure out how to define these click events just once.
(A insert code command would be helpful but does not yet exist)

Any ideas ?

Thanks in advance,
Chris
 

agraham

Expert
Licensed User
Longtime User
But i cannot figure out how to define these click events just once.
You can't. The button_click event must be defined in the B4XPage that owns the button. The code of the event may be the same across pages but the event is a property of each individual page. In Java terms the event is a method of the class of which the page is an instance.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I'm using the same customlayoutdialog on different pages.
What is the difference in the different pages.
Maybe another organisation could be useful, but without knowing the details it's impossible to give a concrete advice.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
You can't. The button_click event must be defined in the B4XPage that owns the button
In the link above, which is a tutorial, @Erel states: We can directly access and manipulate views of other pages.
Does it mean , we can access button events of other B4XPages. I don't know. I am still crawling.
 
Upvote 0

Phayao

Active Member
Licensed User
Longtime User
Great, thank you very much - i will try your ideas and give feedback.
The solution is quite trivial:
Several B4XPages call the same customlayoutdialog, which has many buttons to format a text in the SMMRicheditor inside the dialog.
Instead to define each button action on each page, I call a function in an external code module:
B4X:
Dim re as SMMRicheditor
.....
Sub btnFormat_click
    Dim btn = Sender As Button
    Log("btn tag= "&btn.Tag)
    functions.formatButton(re, btn.tag)
End Sub

In the functions module:
B4X:
public Sub formatButton(re As SMMRichEditor, form As String)
    Select form
        Case "heading"
            re.heading = 2
        Case "bold"
            re.setbold

......

What i was not aware of is that the "transferred" view "re" from the page can be altered by the commands in the functions module.
Maybe a very trivial post, but maybe helpful for some beginners like me.
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
Create a Class with all code and events and use the same class from all pages.
Great minds think alike! I just came on to suggest this and found I was too late :)
This is definitely the best if the pages are sufficiently similar.
 
Upvote 0

Cadenzo

Active Member
Licensed User
Longtime User
What i was not aware of is that the "transferred" view "re" from the page can be altered by the commands in the functions module.
Yes, if you have a simple type like int, short, double, ... you get the value copy in the sub parameter. But more sophisticated types you get as reference. So you get the original and can manipulate it also in the sub.
 
Upvote 0
Top