Android Code Snippet Change colors alpha value to a given color

According to Material Design guidelines it is recommended to set the opacity of a color to a special value instead of making the color lighter or darker. Since the color scheme may change at runtime it is eventually neccessary to adapt the alpha value of a textcolor.
04-11-_2016_17-12-21.jpg

AdjustAlpha

B4X:
...
' Set the opacity for Text
Dim AC As AppCompat
lblTitle.Textcolor = AdjustAlpha( AC.GetThemeAttribute("android:textColorPrimary" ) ,  255 *0.54)
...

' Returns a given color value with adjusted alpha value
Sub AdjustAlpha(intColor As Int, intAlpha As Int) As Int
    Dim argb() As Int
    argb = GetARGB(intColor)
    Return Colors.ARGB(intAlpha, argb(1), argb(2), argb(3))
End Sub
Sub GetARGB(Color As Int) As Int()
    '  From Erel --> https://www.b4x.com/android/forum/threads/get-alpha-red-green-blue.7258/#post-41502
    Dim res(4) As Int
    res(0) = Bit.UnsignedShiftRight(Bit.And(Color, 0xff000000), 24)
    res(1) = Bit.UnsignedShiftRight(Bit.And(Color, 0xff0000), 16)
    res(2) = Bit.UnsignedShiftRight(Bit.And(Color, 0xff00), 8)
    res(3) = Bit.And(Color, 0xff)
    Return res
End Sub

Tags: #MaterialDesign, #Alpha
 
Last edited:
Top