Android Question How I can change the Label properties in running (by code)

Adamdam

Active Member
Licensed User
Longtime User
Dear All,
Greetings,
I create Label in running like:
dim Lbl1 as Label
Lbl1.Initialize("Lbl1")

I can change some Properties (like : color, textcolor,..)

But I need to change some other Label Properties (like : Bold/normal, Corners value, Border,..), there is a way to change these Properties ?

Other way may i make to solve by building one first in layout creator, and make all requested properties, and in the code I'll create others as inherent properties from this one, Question : how can i create new label as a copy from first one ???

thanks in advance.
 

klaus

Expert
Licensed User
Longtime User
But I need to change some other Label Properties (like : Bold/normal, Corners value, Border,..), there is a way to change these Properties ?
Yes.
Bold / normal
B4X:
Lbl1.TypeFace = Typeface.DEFAULT_BOLD
'or
Lbl1.TypeFace = Typeface.DEFAULT
Corners, Border with ColorDrawable
ColorDrawable.Initialize2(Color As Int, CornerRadius As Int, BorderWidth As Int, BorderColor As Int)
B4X:
Dim cdw As ColorDrawable
cdw.Initialize2(Colors.Red, 10dip, 2dip, Colors.Blue)
Lbl1.Background = cdw
 
Upvote 0

Adamdam

Active Member
Licensed User
Longtime User
Many thanks Mr. Klaus, I work well.

Are you have answer for the second question ?
Question : how can I create new label (or any object ) as a copy from first one ???
The first one will be created in layout editor with all requested features/properties.

thanks in advance.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You need to Dim and initialize each object separately and define all properties again.
If you try Label2 = Label1 both objects refer to the same one, and you might get an error.
Something like this:
B4X:
Dim Label1 As Label
Label1.Initialize("")
Activity.AddView(Label1, 10dip, 60dip, 100dip, 40dip)
Label1.Color = Colors.Red
Label1.TextColor = Colors.Yellow
Label1.Text = "Test"
   
Dim Label2 As Label
Label2.Initialize("") 
Activity.AddView(Label2, 120dip, 60dip, 100dip, 40dip)
Label2.Color = Colors.Blue
Label2.TextColor = Colors.Red
Label2.Text = "Text"
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Could this work?
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim label1 As Label
    label1.Initialize("")
    Activity.AddView(label1,0,0,90%x,5%y)
    label1.Background = My_Background(Colors.Blue,5dip,2dip,Colors.White)
    label1.Tag = label1.Width & "," & label1.Height & "," & Colors.blue & "," & 5dip & "," & 2dip & "," & Colors.White
    Dim label2 As Label = Clone_Label(label1.Tag)
    Activity.AddView(label2,0,6%y,90%x,5%y)
    label1.Text = "I AM LABEL NUMBER 1"
    label2.Text = "I AM LABEL NUMBER 2"
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Clone_Label (target As String) As Label
    Dim MyView As Label
    MyView.Initialize("")
    Dim value() As String
    value = Regex.Split(",",target)
    MyView.Width = value(0)
    MyView.Height = value(1)
    MyView.Background = My_Background(value(2),value(3),value(4),value(5))
    MyView.Tag = target
    Return MyView
End Sub

Sub My_Background (backcolor As Int, radius As Int, borderwidth As Int, bordercolor As Int) As ColorDrawable
    Dim cd As ColorDrawable
    cd.Initialize2(backcolor,radius,borderwidth,bordercolor)
    Return cd
End Sub
The code can be for sure improved and corrected to update the parameters in the Tag in a better way.
It's just a starting idea for cloning an existing Label.
 
Upvote 0
Top