Android Code Snippet Sharing the goodness: Some useful methods

Informatix

Expert
Licensed User
Longtime User
Cannot work. rslt must have the same size as lstFrom.
And you can do it with just 2 lines:
B4X:
Dim JO As JavaObject = lstFrom
Dim rslt() As Object = JO.RunMethod("toArray", Null)
 
Last edited:

Informatix

Expert
Licensed User
Longtime User
Should be renamed HasIPAddress because that does not test at all that you have a connection to Internet.
 

Informatix

Expert
Licensed User
Longtime User
An alternative using no extra list:
B4X:
Sub ListRemoveDups(xL As List)
    Dim item As Object
    For i = xL.Size - 1 To 0 Step -1
        item = xL.Get(i)
        If xL.IndexOf(item) <> i Then
            xL.RemoveAt(i)
        End If
       Next
End Sub
 

Mashiane

Expert
Licensed User
Longtime User
Wow, thanks, yes I noted this a few days ago as the other one uses a Length property and the other one a Size Property. You're good!
 

susu

Well-Known Member
Licensed User
Longtime User
Thank you for sharing but B4A have some method already like Trim, Uppercase, Lowercase, Replace... right?
 

Mashiane

Expert
Licensed User
Longtime User
Thank you for sharing but B4A have some method already like Trim, Uppercase, Lowercase, Replace... right?
Hi Susu

Yes, you are right however my first post code section speaks to that exactly and the point is for someone who comes from a VB background where Uppercase is UCase and lowercase is LCase in some instances, thus the definition of such functions here.
 

Mashiane

Expert
Licensed User
Longtime User
This is all great but please credit the original authors
Your comments are noted, however please note the first paragraph of my first post reads, "This is a collection of methods that I have collected here in b4a..." , as much as some of the methods are originally mine, I did not credit anyone, however if you happen to remember the original authors, you are welcome to reply to the specific posts. My intentions are simply to help other coders.
 

Informatix

Expert
Licensed User
Longtime User
For me, your intent was clear.
It's a good idea to create such a repository of useful functions, and try to improve them if we can. It's the purpose of this forum section.
 

Mashiane

Expert
Licensed User
Longtime User
B4X:
'Description: Ensure you have a list with unique values, no duplicates.
'Tag: lists, unique, duplicates
Sub ListAddUniqueItem(lstName As List, itm As String)
    itm = LCase(itm)
    Dim ipos As Int
    ipos = lstName.IndexOf(itm)
    If ipos >= 0 Then Return
    lstName.add(itm)
End Sub
 
Last edited:

Informatix

Expert
Licensed User
Longtime User
I suggest to give it a clearer name. For example: AddIfUnique, or AddIfNotFound.
 

Mashiane

Expert
Licensed User
Longtime User
B4X:
'Description: get a parent path of a file from a complete file path
'Tag: file, path, parent
Sub GetFileParentPath(Path As String) As String
   Dim Path1 As String
   Dim L As Int
   If Path = "/" Then
      Return "/"
   End If
   L = Path.LastIndexOf("/")
   If L = Path.Length - 1 Then
      'Strip the last slash
      Path1 = Path.SubString2(0,L)
   Else
      Path1 = Path
   End If
   L = Path.LastIndexOf("/")
   If L = 0 Then
      L = 1
   End If
   Return Path1.SubString2(0,L)
End Sub
 

Mashiane

Expert
Licensed User
Longtime User
B4X:
'Description: set the text value of a panel label using its tag property
'Tag: label, text, tag
Sub SetPanelLabelTextByTag(pnl As Panel, lblTag As String, lblValue As String)
    For Each v As View In pnl.GetAllViewsRecursive
        If v Is Label Then
            If LCase(v.tag) = LCase(lblTag) Then
                Dim lbl As Label
                lbl = v
                lbl.Text = lblValue
            End If
        End If
    Next
End Sub
 

Mashiane

Expert
Licensed User
Longtime User
B4X:
'Description: set imageview bitmap using its tag property
'Tag: label, text, tag
Sub SetPanelImageViewByTag(pnl As Panel, imgTag As String, imgPath As String, imgName As String)
    For Each v As View In pnl.GetAllViewsRecursive
        If v Is ImageView Then
            If LCase(v.tag) = LCase(imgTag) Then
                Dim img As ImageView
                img = v
                img.Bitmap = LoadBitmapSample(imgPath, imgName,100%x,100%y)
            End If
        End If
    Next
End Sub
 
Last edited:

Mashiane

Expert
Licensed User
Longtime User
B4X:
'Description: find a panel label using its tag property
'Tag: label, tag
Sub FindPanelLabelByTag(pnl As Panel, lblTag As String) As Label
    For Each v As View In pnl.GetAllViewsRecursive
        If v Is Label Then
            If LCase(v.tag) = LCase(lblTag) Then
                Return v
            End If
        End If
    Next
    Return Null
End Sub
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…