Android Code Snippet Available Character Sets

SubName: AvailableCharacterSets

Description: Each device can have different available character sets, this sub will return a list of those available on the device.

B4X:
Sub AvailableCharacterSets As List
    'Create the output list
    Dim L As List
    L.Initialize
    'Initialize CSet to the static class Charset
    Dim CSet As JavaObject
    CSet.InitializeStatic("java.nio.charset.Charset")
    'Get an immutable ordered map of available character sets
    Dim CSetMap As JavaObject = CSet.RunMethod("availableCharsets",Null)
    'Get the keys from the Map as a Set
    Dim Keys As JavaObject = CSetMap.RunMethod("keySet",Null)
    'Get an iterator for the set
    Dim Iterator As JavaObject = Keys.RunMethod("iterator",Null)
    'Loop through the Keys Set and add each item to the output list
    Do While Iterator.RunMethod("hasNext",Null)
        L.Add(Iterator.RunMethod("next",Null))
    Loop
    Return L
End Sub

Usage:
B4X:
Dim CharSets As List
CharSets = AvailableCharacterSets
For Each CS As String In CharSets
    Log(CS)
Next

Depends On: JavaObject

If you want to see how the JavaObject is being applied, have a look at the relevant documentation:

http://developer.android.com/reference/java/nio/charset/Charset.html
http://developer.android.com/reference/java/util/SortedMap.html
http://developer.android.com/reference/java/util/Map.html
http://developer.android.com/reference/java/util/Set.html

The code will also work unchanged on B4j

Tags: B4a B4j Available Character Sets
 
Last edited:
Top