Android Code Snippet Sharing the goodness: Some useful methods

Mashiane

Expert
Licensed User
Longtime User
Hi @Informatix

Someone said to me asking questions when you don't understand is not a sign of weakness but a sign of wisdom. I hope people will bear with me asking so many questions. . As a newbie in b4x, I'm grateful that there are people like you with both experience and the know how and are willingness to help other coders like me. Viva @Informatix!

I hear your explanation and it makes sense. Thanks again.
 

Mashiane

Expert
Licensed User
Longtime User
B4X:
'Description: VB equivalent LTrim method using Regular Expressions
'Tag: LTrim, Regular Expressions
Sub LTrim(s As String) As String
   Dim m As Matcher = Regex.Matcher("^(\s+)", s)
   If m.Find Then
     Return s.SubString(m.GetEnd(1))
   Else
     Return s
   End If
End Sub

'Description: VB equivalent RTrim method using Regular Expressions
'Tag: RTrim, Regular Expressions
Sub RTrim(s As String) As String
   Dim m As Matcher = Regex.Matcher("(\s+)$", s)
   If m.Find Then
     Return s.SubString(m.GetEnd(1))
   Else
     Return s
   End If
End Sub
 

Mashiane

Expert
Licensed User
Longtime User
B4X:
'Description: Function to customize your button with backcolor, textcolor, bordercolor, bordersize and radius
'Tag: button, backcolor, textcolor, radius
Sub Fancy_Button(btn As Button, backColor As Int, textColor As Int, borderSize As Int, cornerRadius As Float, borderColor As Int)
    Dim dr As ColorDrawable
    dr.Initialize2(backColor, cornerRadius, borderSize, borderColor)
    btn.Background = dr
    btn.TextColor = textColor
    btn.Invalidate
End Sub

'Description: BoosStrap default button
Sub BS_ButtonDefault(btn As Button)
    Fancy_Button(btn,Colors.White,Colors.Black,1,2%x,Colors.Gray)
End Sub

'Description: BoosStrap primary button
Sub BS_ButtonPrimary(btn As Button)
    Dim col As Int = Colors.RGB(51,122,183)
    Fancy_Button(btn,col,Colors.white,1,2%x,col)
End Sub

'Description: BoosStrap success button
Sub BS_ButtonSuccess(btn As Button)
    Dim col As Int = Colors.RGB(68,157,68)
    Fancy_Button(btn,col,Colors.white,1,2%x,col)
End Sub

'Description: BoosStrap info button
Sub BS_ButtonInfo(btn As Button)
    Dim col As Int = Colors.RGB(91,192,22)
    Fancy_Button(btn,col,Colors.white,1,2%x,col)
End Sub

'Description: BoosStrap warning button
Sub BS_ButtonWarning(btn As Button)
    Dim col As Int = Colors.RGB(240,173,78)
    Fancy_Button(btn,col,Colors.white,1,2%x,col)
End Sub

'Description: BoosStrap warning button
Sub BS_ButtonDanger(btn As Button)
    Dim col As Int = Colors.RGB(217,83,79)
    Fancy_Button(btn,col,Colors.white,1,2%x,col)
End Sub
 

Mashiane

Expert
Licensed User
Longtime User
B4X:
'Description: Returns map keys as a delimited string
'Tag: map, delimiter, string
Sub MapKeysToMV(m As Map, Delim As String) As String
    Dim sb As StringBuilder
    sb.Initialize
    For Each k As String In m.Keys
        sb.Append(k).Append(Delim)
    Next
    Return RemDelim(sb.tostring,Delim)
End Sub
 

Mashiane

Expert
Licensed User
Longtime User
B4X:
'Description: Return the mobile number of your device using Reflection
'Tag: mobile number
Sub Phone_GetMobileNumber1() As String
    Dim Obj1 As Reflector
     Dim LineNumber As String
     Obj1.Target = Obj1.GetContext
     Obj1.Target = Obj1.RunMethod2("getSystemService", "phone", "java.lang.String")
     LineNumber = Obj1.RunMethod ("getLine1Number")
    Return LineNumber
End Sub
 

Mashiane

Expert
Licensed User
Longtime User
B4X:
'Description: change the phone orientation (irrespective of what was supported)
'Tag: orientation
Sub Phone_ChangeOrientation(LP As String)
    Dim p As Phone
    Select Case LP
    Case "L"
        ' landscape
        p.SetScreenOrientation(0)
    Case "P"
        'potrait
        p.SetScreenOrientation(1)
    End Select
End Sub
 

Mashiane

Expert
Licensed User
Longtime User
B4X:
'Description: Remove an item from a customlistview using its value
'Tag: customlistview, remove item
Sub ListViewRemoveItemByValue(lstView As CustomListView, Value As String)
    Dim lstTot As Int
    Dim lstCnt As Int
    Dim lstStr As String
    lstTot = lstView.GetSize - 1
    For lstCnt = lstTot To 0 Step -1
        lstStr = lstView.GetValue(lstCnt)
        Select Case lstStr
        Case Value
            lstView.RemoveAt(lstCnt)
            Exit
        End Select
    Next
End Sub
 

Mashiane

Expert
Licensed User
Longtime User
Just found this lovely code...
B4X:
'Description: Make an imageview to have rounded corners
' Tag: imageview, rounded corners
Sub ImageView_SetRoundCorners(img As ImageView, imgDir As String, imgPath As String, imgRadius As Int)
    Dim b As Bitmap
      Dim rsie As RSImageEffects
      Dim rsip As RSImageProcessing
    rsip.Initialize
    b = LoadBitmapSample(imgDir, imgPath,100%x, 100%y)
    b = rsip.createScaledBitmap(b, img.width, img.height, False) ' createScaledBitmap seems to produce a better compressed image
    img.Bitmap = rsie.RoundCorner(b, imgRadius) ' round bitmap corners
End Sub

Uses: https://www.b4x.com/android/forum/threads/rsimageprocessing-library.16230/
 

Mashiane

Expert
Licensed User
Longtime User
B4X:
'Description: Sort a map by its keys in ascending order
'Tag: map, sort, asc
Sub SortMapKeys (m As Map, SortAsc As Boolean)
   Private KeysList As List
   Private m2 As Map
   Private key As String
   Private mVal As Object
   Private i As Int
   KeysList.Initialize
   m2.Initialize
  
   ' get all the keys
   KeysList = m.Keys
   KeysList.Sort(True)
  
   For i= 0 To KeysList.Size - 1
       key = KeysList.Get(i)
    mVal = m.Get(key)
    m2.Put(key, mVal)
   Next
  m.Clear
  For Each m2Key As String In m2.Keys
      m.Put(m2Key, m2.Get(m2Key))
  Next
End Sub
 

Mashiane

Expert
Licensed User
Longtime User
B4X:
'Description: Calculate the age of the person at death
'Tag: age, date, death
Sub AgeAtDeath(birthDate As String, deathDate As String) As String
    DateTime.DateFormat = "yyyy-MM-dd"
    Dim Datestart As Long
    Dim DateEnd As Long
    Dim age As String
    Datestart = DateTime.DateParse(birthDate)
    DateEnd = DateTime.DateParse(deathDate)
    Dim p As Period
    p = DateUtils.PeriodBetween(Datestart,DateEnd)
    age = p.Years &" Years, " & p.Months & " months, " & p.Days & " days"
    Return age
End Sub
 

Mark Zraik

Member
Licensed User
Longtime User
Mashiane, your work and others is not wasted. Thank you for putting this together. Thank you to Informatix and others for chiming in and showing better or different ways to get the job done. As we jump from one thing to another in life, it helps to have a collection to at least start from. Thank you!!
 

Ferbis

Active Member
Licensed User
Longtime User
Same opinion as Mark. Thanks Mashiane for your time and work putting all this code routines together; and also thanks to Informatix and others for being there.
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…