Android Question add CustomView to Activity at runtime?

MarkusR

Well-Known Member
Licensed User
Longtime User
hello,
how can i add a custom view (it have the name ViewConsumer) to activity at runtime?

Activity.AddView(View1

Consumer is a class with data.

B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim Views As List
    Dim Timer1 As Timer

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

    Activity.Title = "Consumers"
    Activity.AddMenuItem("Settings","Settings")

    Views.Initialize
    Dim Consumer1 As Consumer
    Dim i As Int
    Dim y As Int = 0
    For i = 1 To 10
        Consumer1.Initialize
        Consumer1.Dummy("Name " & i)
        Dim View1 As ViewConsumer
        View1.Set(Consumer1)
        View1.GetBase.Tag = Consumer1
        Activity.AddView(View1 ,0,y,100%x,100dip)      
        y=y+100dip
        Views.Add(View1)
    Next

    Timer1.Initialize("Timer1",1000)
    Timer1.Enabled = True

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Timer1_Tick

    For Each View1 As ViewConsumer In Views
       
        Dim Consumer1 As Consumer : Consumer1 = View1.GetBase.Tag
        If Consumer1.Data.On = True Then
            Consumer1.Update      
            View1.Set(Consumer1)
        End If
       
    Next
   
End Sub

Sub Settings_Click
   
    Log("Settings_Click")
   
End Sub

the custom view "ViewConsumer"
B4X:
'Custom View class
'#Event: ExampleEvent (Value As Int)
'#DesignerProperty: Key: BooleanExample, DisplayName: Boolean Example, FieldType: Boolean, DefaultValue: True, Description: Example of a boolean property.
'#DesignerProperty: Key: StringWithListExample, DisplayName: String With List, FieldType: String, DefaultValue: Sunday, List: Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday
#DesignerProperty: Key: Name, DisplayName: Name, FieldType: String, DefaultValue: Name ...
#DesignerProperty: Key: Watt, DisplayName: Watt, FieldType: Int, DefaultValue: 1, MinRange: 0, MaxRange: 9999, Description:
'#DesignerProperty: Key: ColorExample, DisplayName: Color Example, FieldType: Color, DefaultValue: 0xFFCFDCDC, Description: You can use the built-in color picker to find the color values.
'#DesignerProperty: Key: DefaultColorExample, DisplayName: Default Color Example, FieldType: Color, DefaultValue: Null, Description: Setting the default value to Null means that a nullable field will be displayed.
Sub Class_Globals
    Private mEventName As String 'ignore
    Private mCallBack As Object 'ignore
    Private mBase As Panel
    Private Const DefaultColorConstant As Int = -984833 'ignore

    Private LabelName As Label
    Private LabelDateStartStop As Label
    Private LabelWatt As Label
    Private LabelDurationHours As Label
    Private LabelSumPrice As Label
    Private LabelOnOff As Label

    Private ToggleButtonOnOff As ToggleButton

End Sub

Public Sub Initialize (Callback As Object, EventName As String)
    mEventName = EventName
    mCallBack = Callback
End Sub

Public Sub DesignerCreateView (Base As Panel, Lbl As Label, Props As Map)
    mBase = Base
   
    Sleep(0)
    Base.LoadLayout("Consumer")
   
End Sub

Public Sub GetBase As Panel
    Return mBase
End Sub

public Sub Set(Consumer1 As Consumer)
   
    LabelName.Text = Consumer1.Data.Name
    LabelDateStartStop.Text = DateTime.Date(Consumer1.Data.DateOn) & " - " & DateTime.Date(Consumer1.Data.DateOff)
    LabelWatt.Text = Consumer1.Data.Watt
    LabelDurationHours.Text = Consumer1.Data.SumDurationHours & "(+ " & Consumer1.Data.DurationHours & ")"
    LabelSumPrice.Text = Consumer1.Data.SumPrice
    If Consumer1.Data.On = True Then
        LabelOnOff.Text = "On"
    Else
        LabelOnOff.Text = "Off"
    End If
   
End Sub

Sub ToggleButtonOnOff_CheckedChange(Checked As Boolean)
   
    Dim Consumer1 As Consumer
    Consumer1 = mBase.Tag
   
    If Checked Then
        Consumer1.Start
    Else
        Consumer1.Stop
    End If
   
    Set(Consumer1)
       
End Sub

consumer class
B4X:
Sub Class_Globals
   
    Type ConsumerData(Name As String,Watt As Float,On As Boolean, DateOn As Long,DateOff As Long,DurationHours As Float,SumDurationHours As Float, SumPrice As Float)
    Dim Data As ConsumerData
   
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
   
    Data.Initialize
   
End Sub

Sub Start
   
    Data.On = True
    Data.DateOn = DateTime.Now

    Update
   
End Sub

Sub Update

    Data.DateOff = DateTime.Now
    Data.DurationHours = ( (Data.DateOff - Data.DateOn) / DateTime.TicksPerHour)

    Data.SumPrice = (Data.Watt / 1000.0) * (Data.SumDurationHours + Data.DurationHours) * 0.3034

End Sub

Sub Stop

    Update

    Data.SumDurationHours = Data.SumDurationHours + Data.DurationHours
    Data.DurationHours = 0
   
    Data.SumPrice = (Data.Watt / 1000.0) * Data.SumDurationHours * 0.3034
   
    Data.On = False
   
End Sub
   
Public Sub Dummy(Name As String)

    Data.Name = Name
    Data.DateOn = DateTime.Now
    Data.DateOff = DateTime.Now
    Data.DurationHours = 0.0
    Data.SumDurationHours = 0.0
    Data.SumPrice = 0.0
    Data.Watt = 500
   
End Sub
 
Top