B4A 2.70 BETA1 - Designer + custom views question

NJDude

Expert
Licensed User
Longtime User
I'm trying to understand the new designer and the use of custom views, on the "Sample252.zip" I have a very simple class which works fine, now, let's say I want to "convert" it to 2.70, and there's where I'm getting the difficulties understanding, on the "Sample270.zip" I've made a similar class but created it from scratch using B4A 2.70 which works fine too, however, I don't grasp how would you let the user know how to initialize it and use it in case the class has been compiled as a Library.

I do understand that you need to add those 2 SUBs on 2.70 but there's already 1 Sub called INITIALIZE (in Sample252) so , unless you re-write your class I don't see how it would work, I'm confused here.

Am I missing something?
 

Attachments

  • Sample252.zip
    9.8 KB · Views: 165
  • Sample270.zip
    11.1 KB · Views: 160

stevel05

Expert
Licensed User
Longtime User
You're right that the initialize sub can not be used as it was if you want to add the Custom View to the designer, I suppose you could say it's is a reserved sub for this use.

Any other initialization needs to be done in another sub.

As I understand it the initialize and the DesignerCreateView subs are called by the Activity (or Panel) LoadLayout method. So don't need to be called in this instance.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Erel,

You probably have, but I wondered if you had considered creating a new module for custom views which could separate them from classes and provide the required template subs?

And would you see a custom view containing more functionality than just managing the view?.

-------------------
Sent via Tapatalk
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Another question, (not opening another thread since is somewhat related to my original post), if you create a class/lib using the designer in 2.70, that means that you cannot initialize it manually?, for example, like users who do not like to use the designer, if that's the case, I see that as a limitation, since you will be forcing users to use the designer (which I love by the way) but the users who prefer writing code manually won't be happy, I say this because I've written a whole series of custom views (in fact, one of the forum members is using one on his app) and I know his style of programming is to do his layouts by code, I think he would be very unhappy if I re-release that view for 2.70.

I personally like using the designer, and encourage everyone to use it too, in my opinion is the strong point of B4A but in this case this new feature makes things a little inflexible and kind of breaks the way B4A works, just my humble opinion.

NOTE: A workaround would be to write a "designer" and "manual" version for the same lib in one file, I have done it and works just fine, basically you have to duplicate the code.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There is no need to duplicate any code. You also don't need to create a new class. Take CustomListView for example. In order to add designer support for it, all you need to do is add the DesignerCreateView sub:
B4X:
Public Sub Initialize (vCallback As Object, vEventName As String)
   sv.Initialize2(0, "sv")
   items.Initialize
   panels.Initialize
   dividerHeight = 2dip
   EventName = vEventName
   CallBack = vCallback
   sv.Color = 0xFFD9D7DE 'this sets the dividers color
   Dim r As Reflector
   Dim idPressed As Int
      idPressed = r.GetStaticField("android.R$drawable", "list_selector_background")
    r.Target = r.GetContext
    r.Target = r.RunMethod("getResources")
   pressedDrawable = r.RunMethod2("getDrawable", idPressed, "java.lang.int")
   DefaultTextColor = Colors.White
   DefaultTextSize = 16
   DefaultTextBackgroundColor = Colors.Black
End Sub

'Returns a view object that holds the list.
Public Sub AsView As View
   Return sv
End Sub

Public Sub DesignerCreateView(base As Panel, lbl As Label, props As Map)
   base.AddView(sv, 0, 0, base.Width, base.Height)
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
To convert your class and add designer support you can change the code to:
B4X:
Public Sub Initialize(Module As Object, EventName As String)
    mModule = Module
   mEventName = EventName
End Sub

Public Sub AddToParent(Parent As Panel,  Left As Int, Top As Int, Width As Int, Height As Int, Text As String)
   myButton.Initialize("myButton")
   Parent.AddView(myButton, Left, Top, Width, Height)
   myButton.Text = Text
End Sub
Public Sub DesignerCreateView(base As Panel, lbl As Label, props As Map)
   AddToParent(base, 0, 0, base.Width, base.Height, lbl.Text)
End Sub

User code to add programmatically:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    LittleButton.Initialize(Me, "Button")
   LittleButton.AddToParent(Activity, 50dip, 20dip, 120dip, 50dip, "Hello")
End Sub
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
I've been playing with the designer + custom views in 2.70 and I'm having some issues, I would like to be able to let the user choose between manual or designer when using a class/library, I got it working just fine but when I use a layout it doesn't show and I see no errors either.

The attached sample is supposed to do that, if you want to use the designer then uncomment line #32 and comment lines #34 and #35 otherwise do the opposite.

I tried several ways but I was unsuccessful, so, I'm posting the code here and waiting for guidance.

Thanks.
 

Attachments

  • TestCode270.zip
    11.1 KB · Views: 147
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I've changed it about a bit but I think this is what you are after.
 

Attachments

  • cv1.zip
    11.2 KB · Views: 155
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Well, thank you, but not exactly what I'm looking for, if you uncomment lines #38 - #42 in the Main module you'll get errors, that's the problem.

What I'm looking for is to use either the designer or manual initialization.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Try this one, you have to manage the Base panel as well if the view is added by designer as it's sized automatically, may be more trouble than it's worth in a big Custom View.

But then again I wouldn't really expect a custom view to be edited too much once it was built.

Un comment references to mBase and CustomView1 as well as the activity, just the two LittleButton lines to comment still.
 

Attachments

  • cv1.zip
    11.4 KB · Views: 158
Last edited:
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Ok, the code posted is not doing what I'm after, this is what I like to do (if it's possible)

If the user wants to use the designer should use this code:
B4X:
Sub Activity_Create(FirstTime As Boolean)

Activity.LoadLayout("Main") 'Designer only

LittleButton.Text = "Designer"
LittleButton.Width = 50%x
LittleButton.Height = 50dip
            
End Sub

And if the user want to code manually then this code:
B4X:
Sub Activity_Create(FirstTime As Boolean)

LittleButton.Initialize(Me, "TheButton") 'Manual only
LittleButton.AddToParent(Activity, 50dip, 20dip, 120dip, 50dip) 'Manual only

LittleButton.Text = "Designer"
LittleButton.Width = 50%x
LittleButton.Height = 50dip
            
End Sub

So, unless I'm missing something or it's not possible, the code is not working.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
This is a close as I can get it, you still have to manage the Base in the designer added version Main Activity.

I have created a second Activity Manual for easy comparison.

Edit: File removed no benefit in leaving it.
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
D'oh the penny's just dropped, hang on a bit and I'll have another go.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
This is better, it's much easier when the brain functions that golden 5 mins a day!
 

Attachments

  • cv3.zip
    11.9 KB · Views: 159
Upvote 0

stevel05

Expert
Licensed User
Longtime User
This one respects the default text attributes passed by the label. An oversight at 4am yesterday. Although the default text colour on my Bluestacks test environment is the same colour as the button background so beware.
 

Attachments

  • cv4.zip
    11.9 KB · Views: 161
Last edited:
Upvote 0
Top