Android Question B4XColorTemplate pre-set color

PSEAD

Member
Licensed User
Longtime User
How would you set the initial color in the dialog? The ColorTemplate.SelectedColor is read only and I would like the picker to open on the color that was set previously.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The simplest way is to reuse the same template each time. Try it with the XUI Views Example. It starts with the previously selected color.

If you mean that you want to save the selected color then you need to keep the SelectedHSVColor. Try this example and rotate the device. Note that the preset color is kept.
B4X:
Sub Process_Globals
   Private PreviouslySelectedColor() As Object
   Private xui As XUI
End Sub

Sub Globals
   Dim dialog As B4XDialog
   Dim ColorTemplate As B4XColorTemplate
End Sub

Sub Activity_Create(FirstTime As Boolean)
   dialog.Initialize(Activity)
   ColorTemplate.Initialize
   If PreviouslySelectedColor.Length > 0 Then
       ColorTemplate.SelectedHSVColor = PreviouslySelectedColor
   End If
End Sub

Sub Activity_Click
   Wait For (dialog.ShowTemplate(ColorTemplate, "OK", "", "CANCEL")) Complete (Result As Int)
   If Result = xui.DialogResponse_Positive Then
       Activity.Color = ColorTemplate.SelectedColor
       PreviouslySelectedColor = ColorTemplate.SelectedHSVColor
   End If
End Sub
 
Upvote 0

PSEAD

Member
Licensed User
Longtime User
If I have the color stored in a DB as an int, how do I convert this to the HSVcolor?
 
Upvote 0

PSEAD

Member
Licensed User
Longtime User
I did this and it seems to work :
B4X:
#If Java

import android.graphics.Color;

public float[] gethsv(int colorin) {
    float[] hsv = new float[3];
    Color.colorToHSV(colorin, hsv);
    return hsv;
}

#End If

Sub gethuecolor(Colorin As Int) As Float()
    Dim NativeMe As JavaObject
    NativeMe.InitializeContext
    Return NativeMe.RunMethod("gethsv", Array As Object(Colorin))
End Sub

Sub btn_setbackcolor_Click
    Dim temp(3) As Float
    Dim hsvset(4) As Object
    temp = gethuecolor(something.BackColor)
    hsvset(0) = temp(0)
    hsvset(1) = temp(1)
    hsvset(2) = temp(2)
    hsvset(3) = 255
    ColorTemplate.SelectedHSVColor=hsvset
    Wait For (Dialog.ShowTemplate(ColorTemplate, "OK", "", "CANCEL")) Complete (Result As Int)
    If Result = XUI.DialogResponse_Positive Then
        something.BackColor = ColorTemplate.SelectedColor
    End If
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…