Android Question Get a color defined in the manifest

Cristian Rufino

Member
Licensed User
Longtime User
Hello

This is part of the application manifest:

CreateResource(values, colors.xml,
<resources>
<color name="actionbar">#ff039be5</color>
<color name="statusbar">#ff006db3</color>
<color name="textColorPrimary">#ffffffff</color>
<color name="navigationBar">#ff006db3</color>

</resources>)

How do I get the value of "textColorPrimary" from code?
 
Solution
Try this code
B4X:
    Dim bc As ByteConverter
    Dim i(1) As Int
    i(0)=GetColorFromResources("textColorPrimary")
    Log(bc.HexFromBytes(bc.IntsToBytes(i)))
...
Sub GetColorFromResources(ColorName As String) As Int
    Dim jo As JavaObject
    Dim context As JavaObject
    context = jo.InitializeContext
    Dim resources As JavaObject
    resources = context.RunMethod("getResources", Null)
    Dim colorId As Int
    colorId = resources.RunMethod("getIdentifier", Array As Object(ColorName, "color", context.RunMethod("getPackageName", Null)))
    If colorId > 0 Then
        Return resources.RunMethod("getColor", Array As Object(colorId))
    Else
        Return -1 'default color
    End If
End Sub

teddybear

Well-Known Member
Licensed User
Try this code
B4X:
    Dim bc As ByteConverter
    Dim i(1) As Int
    i(0)=GetColorFromResources("textColorPrimary")
    Log(bc.HexFromBytes(bc.IntsToBytes(i)))
...
Sub GetColorFromResources(ColorName As String) As Int
    Dim jo As JavaObject
    Dim context As JavaObject
    context = jo.InitializeContext
    Dim resources As JavaObject
    resources = context.RunMethod("getResources", Null)
    Dim colorId As Int
    colorId = resources.RunMethod("getIdentifier", Array As Object(ColorName, "color", context.RunMethod("getPackageName", Null)))
    If colorId > 0 Then
        Return resources.RunMethod("getColor", Array As Object(colorId))
    Else
        Return -1 'default color
    End If
End Sub
 
Upvote 0
Solution

Cristian Rufino

Member
Licensed User
Longtime User
Try this code
B4X:
    Dim bc As ByteConverter
    Dim i(1) As Int
    i(0)=GetColorFromResources("textColorPrimary")
    Log(bc.HexFromBytes(bc.IntsToBytes(i)))
...
Sub GetColorFromResources(ColorName As String) As Int
    Dim jo As JavaObject
    Dim context As JavaObject
    context = jo.InitializeContext
    Dim resources As JavaObject
    resources = context.RunMethod("getResources", Null)
    Dim colorId As Int
    colorId = resources.RunMethod("getIdentifier", Array As Object(ColorName, "color", context.RunMethod("getPackageName", Null)))
    If colorId > 0 Then
        Return resources.RunMethod("getColor", Array As Object(colorId))
    Else
        Return -1 'default color
    End If
End Sub

It works, thanks
 
Last edited:
Upvote 0
Top