Android Question Custom/Native View Designer Properties

Guenter Becker

Active Member
Licensed User
Hello, hope you are fine.

Today I have a question about developing a Custom View.
For example if I like to make a custom view on base on an EditTextbox to enhance the native views features I know how to do that with the custom view xui class. But if I go to use the custom view in the designer most of the basic properties of the native view like typeface or alignment a.s.o. are lost (no acccess). I declared the native view as public global inside of the custom view class but that is also not the solution for access. I can not believe that the only way to get access to the native properties is to write equal custom designer properties and code.

The question is what is the best b4a way to use the native view in a custom view class and to have access to all native views designer properties?
 

sfsameer

Expert
Licensed User
Longtime User
Hello,

I think you will a great deal of tutorials once you use the search bar :)

i think what you are looking for :

for me i always create viewing dynamically because i can customize the views according to my needs :)
 
Upvote 0

Guenter Becker

Active Member
Licensed User
Sorry but that ist not what I am looking for! I know that I can set custom events by placing them as code in the class. But exact that is my question. Is there no other way? I looked in the forum to find an answer but i did not found the solution. And I read also the articke your link is pointing to. For example looking to the custom control MaskedEdit Textbox I am able to set the text properties if I am using the control in the designer. The question is are the text properties coded width custom properties in the custom class or is there a transparent way to give direct access to the nativ EditTextcontrol that is used in the MaskedEditText.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I can not believe that the only way to get access to the native properties is to write equal custom designer properties and code.
As B4X does not support inheritance I'm afraid that is the way to do it - or, as you have done, expose the base view as a public field so you an assign it to a variable of the appropriate type and then manipulate it.
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
If I understand your question, you can do it either way.

For example, you have a Custom Class which has 2 edit text fields and a label (edit1,edit2 and label1)


Option 1: Write wrapper methods

B4X:
public Sub setAllControlData(title As String, field1 As String, field2 As String)
    label1.Text = title
    edit1.Text = field1
    edit2.Text = field2
End Sub

public Sub getTitle As String
    Return label1.text
End Sub

public Sub getField1 As String
    Return edit1.text
End Sub

public Sub getField2 As String
    Return edit2.text
End Sub

public Sub setAllFont(tfont As B4XFont,efont As B4XFont)
    label1.font = tfont
    edit1.Font = efont
    edit2.Font = efont
End Sub

Option2 Publically expose the controls
B4X:
Sub Class_Globals
    Public edit1 As B4XView
    Public edit2 As B4XView
    Public label1 As B4XView


    Private mEventName As String 'ignore
    Private mCallBack As Object 'ignore
    Public mbase As B4XView 'ignore
end sub

Now your code can access each control individually.

Option1.
Advantages
  • You can hide the implementation from the rest of the code.
  • only one method call required to perform (possibly) complex operations
Disadvantages
  • Don't have fine control over the sub controls e.g. you cannot set the font of the 2 edit fields to different things

Option 2
Advantages
  • Can access the internal control individually allowing finer control

disadvantages
  • Not as easy to change the implementation
  • May need multiple calls to perform certain operations e.g. change the font or set the text.

I would say that there isn't a "best" solution. It would seem that most controls provide a blend of both.


As far as fields in the designer go, you need to propagate this information in the DesignerCreateView function.

the lbl object contains the items from the Text Properties section and also the text
1610296940410.png


B4X:
Public Sub DesignerCreateView (Base As Object, lbl As Label, Props As Map)

' Create the label and edit controls here


    Private tfnt As B4XFont
    tfnt = xui.CreateFont(lbl.Text.font,lbl.TextSize/2)
    Private efnt As B4XFont
    efnt = xui.CreateFont(lbl.Text.font,lbl.TextSize)
    
    
    ' initialise using the label information
    setAllFont(tfnt,efnt)
    setAllControlData(lbl.text,"","")

end sub

If you are loading a layout with your sub controls then it is slightly more complicated but the same method applies.
 
Upvote 0

Guenter Becker

Active Member
Licensed User
Thank you very much. All the shown ways implement that you have to write code for accessing the controls. Maybe inside of the custom class or outside in the parent form. If there is no other One who has an Idea I understood that there is no other way to get direct access without coding. For me I think I will go the way to rebuild the nativ controls properties by coding equal custom designer properties. I think this will mimmick it for the users to have the same way to interact with the properties as they have using nativ controls only difference is they have to look in the custom properties area.

Stay well and far way from corona.
 
Upvote 0
Top