Android Code Snippet Sharing the goodness: Some useful methods

Mashiane

Expert
Licensed User
Longtime User
B4X:
'Description: find a panel imageview using its tag property
'Tag: label, text, tag
Sub FindPanelImageViewByTag(pnl As Panel, imgTag As String) As ImageView
    Return FindViewByTag(pnl, imgTag)
End Sub

'Description: find any view by its tag property
'Tag: view, search, find, tag
Sub FindViewByTag(Parent As Panel, Name As String) As View
For Each v As View In Parent.GetAllViewsRecursive
     If Name = v.Tag Then Return v
Next
Return Null
End Sub
 

Informatix

Expert
Licensed User
Longtime User
Note that the use of LoadBitmap is strongly discouraged as it uses a lot more memory than needed in some cases. LoadBitmapSample should always be used instead.
Much better: my AcceleratedSurface library provides a LoadScaledBitmap function that uses even less memory and fix the issues with density of LoadBitmapSample.
 

Mashiane

Expert
Licensed User
Longtime User

Wow, I like, I will check it out. My current app uses a lot of bitmaps and this will be very useful. Ta!
 

Mashiane

Expert
Licensed User
Longtime User
B4X:
'Description: Get the version name of your app (Main > VersionName)
'Tag: app version name
Sub GetVersionName() As String
    Dim pm As PackageManager
    Dim packageName As String
    packageName = GetPackageName
    Dim AppVersion As String
    AppVersion = pm.GetVersionName(packageName)
    Return AppVersion
End Sub
 

Mashiane

Expert
Licensed User
Longtime User
B4X:
'Description: Get the version code of your app (Main > VersionCode)
'Tag: app, version code
Sub GetVersionCode() As Int
    Dim pm As PackageManager
    Dim packageName As String
    packageName = GetPackageName
    Dim AppVersion As String
    AppVersion = pm.GetVersionCode(packageName)
    Return AppVersion
End Sub
 

Informatix

Expert
Licensed User
Longtime User
Same as above. Application.VersionName...
 

Mashiane

Expert
Licensed User
Longtime User
B4X:
'Description: Get the label of your application (Main > ApplicationLabel)
'Tag: application label
Sub GetApplicationLabel() As String
    Dim pm As PackageManager
    Dim packageName As String
    packageName = GetPackageName
    packageName = pm.GetApplicationLabel(packageName)
    Return packageName
End Sub
 

Mashiane

Expert
Licensed User
Longtime User
B4X:
'Description: copy all images from dirassets to specific folder
'Tag: dirassets, copy
Sub ExtractImagesFromDirAssets()
    ' get the app label
    Dim al As String = Application.LabelName
    ' create an internal folder to store the contents
    File.MakeDir(File.DirInternal, al)
    Dim fTot As Int
    Dim fCnt As Int
    Dim fStr As String
    Dim lst As List
    Dim ext As List
    Dim ext1 As String
    ext.Initialize
    ext.Add("png")
    ext.Add("jpg")
    ext.Add("gif")
    ' get all the list of files under dirassets
    lst = File.listfiles(File.DirAssets)
    fTot = lst.Size - 1
    For fCnt = 0 To fTot
        ' get the file name
        fStr = lst.Get(fCnt)
        ' get the file extension
        ext1 = GetFileExt(File.DirAssets & "/" & fStr)
        ' if this extension exists in ours, save file
        If ListItemExist(ext,ext1) = True Then
            If File.Exists(File.DirInternal, al & "/" & fStr) = False Then File.Copy(File.DirAssets, fStr, File.DirInternal, al & "/" & fStr) 
        End If
    Next
End Sub
 
Last edited:

Informatix

Expert
Licensed User
Longtime User
B4X:
' Description: Use LoadBitmap function to return an image from DirAssets
' if the image does not exist, an alternative image can be used
If the image does not exist in the assets, then just add it or don't try to access it. I don't understand the usefulness of this function. You're supposed to know what you put in your Assets folder and what is in the internal folder (nobody else but you can put things here).

And I don't really understand why you want to put all assets images in the internal folder. Reading them from the folder is slightly faster, but you have to copy them from the assets and take room on the disk. Is there a real benefit?
 

Informatix

Expert
Licensed User
Longtime User
My advice? I never advised to do that. I just gave a theoretical point of view that does not take into account the time to copy the bitmap. My advices would be to avoid LoadBitmap and to copy to DirInternalCache rather than DirInternal. But, anyway, I'd like to understand why you posted this function because, for me, it's just personal code that serves only your purpose. Why should I use it? What's the usefulness for anyone else? And I'm curious to know whether there's a benefit to copy and load a bitmap from a folder rather than decoding it from the assets. It seems to me that's slower. No?
 

Informatix

Expert
Licensed User
Longtime User
Ok but don't forget that's a forum where people are free to speak and give their opinion, and that you published in a section where we share interesting code. It's not a personal repository. It is legitimate to ask the possible uses of such a code, which are not obvious in this case, and to wonder whether the implementation of an idea proved to be performant. I don't know what you understood in my posts and I really don't care now.
 

Mashiane

Expert
Licensed User
Longtime User
B4X:
'Description: Get an image from a device directory, if it does not exist
'get an alternative image from dirassets
'Tag: dirassets, image
Sub GetImageOrAlternative(original As String, alternative As String) As Bitmap
    If Len(original) > 0 Then
        If InStr(original,".") = -1 Then original = original & ".jpg"
    End If
    If Len(alternative) > 0 Then
        If InStr(alternative,".") = -1 Then alternative = alternative & ".jpg"
    End If
    ' the image is not in dirassets, find if the original exists internally
    If File.Exists(File.DirInternal, AppTitle & "/" & original) = True Then
        Return LoadBitmapSample(File.DirInternal, AppTitle & "/" & original, 100%x, 100%y)
    Else
        ' the image does not exist internally, load alternative from dirassets
        Return LoadBitmapSample(File.DirAssets, alternative, 100%x, 100%y)
    End If
End Sub
 

Informatix

Expert
Licensed User
Longtime User
Hi, what's the easiest method then to check if there is an internet connection?
It's a complicated question because the only answer that comes to mind is: it depends. For example, if you want to connect to a server, the most clever thing to do is to try to connect to that server. No need to check anything before. If you want to access internet to check whether your server is up, then you have to ping it (or any other mean to test its availability if you disabled ICMP). No need to check anything else. I tried to explain many times in this forum why pinging the Google servers (which is a common test that you will see here and there) when the application starts is stupid but obviously people do not listen so I won't try to convince you. AFAIK, no browser (Chrome, Opera, Firefox, etc.) does that. There's a case where this Google check can be useful as explained by an user on this forum who helps customers to fix their internet connection but it's not the common use.
You can have many uses of Internet: for email, for games, for video... And you don't know sometimes the Ip address of the server to reach. In this case, just call the function that enables the connection or sends/receives the data and see whether something happens. Your connection may be broken or unavailable at a given time for various reasons so you should never assume that a check at a given time means something for a later time. You can access the network at a given place and lose the connection if you move. Same thing with the public IP address. Having one does not mean that you have currently a connection to internet. It just means that you got one sometimes in the past. Your function had another problem of reliability: it did not verify whether the returned address was a valid address for internet (e.g. 192.168.0.14 is not an internet address, it's an address given by a local router, like the wifi router at home).
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…