Android Question Trying to understand custom views

MitchBu

Well-Known Member
Licensed User
Longtime User
I am trying to wrap my head around custom views with designer support.

I read several times
https://www.b4x.com/android/forum/threads/b4x-custom-views-with-enhanced-designer-support.62488/

And tried to apply the description, but am getting nowhere. I know how to add a DesignerCreateView such as below, but after that, how do I create the actual custom view ?

There are several examples posted in the thread, but they are all B4J so I cannot look at them.

B4X:
#Event: ExampleEvent (Value As Int)
#DesignerProperty: Key: BooleanExample, DisplayName: Boolean Example, FieldType: Boolean, DefaultValue: True, Description: Example of a boolean property.
#DesignerProperty: Key: IntExample, DisplayName: Int Example, FieldType: Int, DefaultValue: 10, MinRange: 0, MaxRange: 100, Description: Note that MinRange and MaxRange are optional.
#DesignerProperty: Key: StringWithListExample, DisplayName: String With List, FieldType: String, DefaultValue: Sunday, List: Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday
#DesignerProperty: Key: StringExample, DisplayName: String Example, FieldType: String, DefaultValue: Text
#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 EventName As String 'ignore
    Private CallBack As Object 'ignore
    Private mBase As Panel
    Private Const DefaultColorConstant As Int = -984833 'ignore
End Sub

Public Sub Initialize (vCallback As Object, vEventName As String)
    EventName = vEventName
    CallBack = vCallback
End Sub

Public Sub DesignerCreateView (Base As Panel, Lbl As Label, Props As Map)
    mBase = Base
  
End Sub

Public Sub GetBase As Panel
    Return mBase
End Sub
 
Last edited:

Cableguy

Expert
Licensed User
Longtime User
The "custom view" container in the designer is just that, a container.
The actual view must be constructed by a class. The visual part of it may be constructed using the normal views form the designer and even adding other custom views... Then the created layout can be loaded into your custom view base panel.
 
Upvote 0

MitchBu

Well-Known Member
Licensed User
Longtime User
The "custom view" container in the designer is just that, a container.
The actual view must be constructed by a class. The visual part of it may be constructed using the normal views form the designer and even adding other custom views... Then the created layout can be loaded into your custom view base panel.

That is interesting. From the documentation, I was under the impression that one had to do everything in code.

How do you load a layout in the custom view base panel ?
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
just like you do to any other panel, I'm sure Klaus has it mentioned in his beginner's guide.

To be able to use the base panel however you should declare a new Panel ( usually we call it mPanel) and in the initialize sub do something like mBase = Base.
This way all properties of Base Panel are accessible using mBase.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
If you need more help, I can create a simple CustomView this afternoon consisting of a label and a button, this way you can explore and build on it
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
For me, adding the other views by code has an advanrage, the customview is indepenant from other files.
If you use a customview needing a layout you must provide it in every project where you use it.
Then if you compile the customview to a library you must provide the layout file, you don't need it if you add the views by code.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
For me, adding the other views by code has an advanrage, the customview is indepenant from other files.
If you use a customview needing a layout you must provide it in every project where you use it.
Then if you compile the customview to a library you must provide the layout file, you don't need it if you add the views by code.
I prefer it too, but in this case, the main objective is to understand how CustomViews are created/implemented, so a layout is one choice. Also, if the CV is compiled into a Lib, the layout file can easily be included in the final file
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
As always, you are correct, Klaus...
 
Upvote 0

MitchBu

Well-Known Member
Licensed User
Longtime User
For me, adding the other views by code has an advanrage, the customview is indepenant from other files.
If you use a customview needing a layout you must provide it in every project where you use it.
Then if you compile the customview to a library you must provide the layout file, you don't need it if you add the views by code.

That would be superb. Nothing like hands on to better understand.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Make sure note to limit it to a single member.
Could be understand wrong :D

again (less words of it)
"note to limit it to a single member."

PD: Sorry, i could not resist
 
Upvote 0
Top