Wish Ability to add tooltips to views as a property in designer or code

Dave O

Well-Known Member
Licensed User
Longtime User
Android 8+ adds the ability to attach a text tooltip to a view. This post shows some wrapper code to do this with JavaObject.

Just like elevation has been added for panels, I'd love to see tooltips added as a declarative property on views, so we could define them using the Designer or in simple code (e.g. aView.tooltip = "Hello world").

This would work on Android 8+, and be ignored for earlier versions.

Thoughts?
 

stevel05

Expert
Licensed User
Longtime User
The new designer script extension is perfect for this type of thing. take a look at my DSE SetToggleGroup code snippet. It's for B4j, but you could easily use the code in the post you linked to to add the tooltips for Android.
 

stevel05

Expert
Licensed User
Longtime User
I fancied a change for 10 mins so here is my take:
B4X:
'Parameters: Text As String, 1 view
'Code in DesignerScript:{class}.SetTooltip("Tooltip", Button1)
Public Sub SetTooltip(DesignerArgs As DesignerArgs)
    Dim p As Phone
    If p.SdkVersion >= 26 Then
        Dim Text As String = DesignerArgs.Arguments.Get(0)
        Dim viewJO As JavaObject = DesignerArgs.GetViewFromArgs(1)
        viewJO.RunMethod("setOnLongClickListener", Array As Object(Null))   'remove any longClick listener
        viewJO.RunMethod("setTooltip", Array As Object(Text))
    End If
End Sub

Then just add the code to the designer script for your layout.

Code in DesignerScript: {class}.SetTooltip("Tooltip", Button1)

{class} is wherever you put the sub, it can go in any class you like. It can go in B4xMainPage, or an existing Utils class if you have one, or it's own separate class.
 
Last edited:

Dave O

Well-Known Member
Licensed User
Longtime User
Thanks Steve!

I confess that I haven't dug into DesignerScript extensions (DSE) yet because when I read Erel's summary of them, I didn't really understand how they would help me write apps. I might need to revisit that.

On the other hand, I think that adding it as a first-class property (in the Designer and in code) would make it more discoverable and easier to use for everyone, especially those less experienced in B4A. I see it as akin to the Padding property, where we had to use wrapper code until it got promoted to a standard property.

Cheers!
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
I understand what you're saying and your wish is still there for Erel to see. In an attempt to make the code more discoverable, I will create a separate thread for this snippet so that it's not buried and undiscoverable for everyone else for the time being.
 
Top