B4J Tutorial [B4X] Custom Views with Enhanced Designer Support

Status
Not open for further replies.

Hugh Thomas

Member
Licensed User
Longtime User
Thanks, but the QUOTE keyword doesn't seem to be recognized in the #DesignerProperty directive.
 

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

created a simple CustomView NumericSpinner. The source code is well commented.

Has been a good learning on how to change Properties and set the position of the CustomView.

 

Attachments

  • B4JHowToCustomViewNumericSpinner.zip
    5.7 KB · Views: 838

Hugh Thomas

Member
Licensed User
Longtime User
Hi,

I'm writing a Custom View, and am having a problem. I want to create a custom view that has a "rotate" option that will rotate the view so that you can have vertical text.

For example, if you wanted the custom view running down the left hand side of the screen, you would create the view with a layout of left=0,top=0,width=50dip,height=100%y. When you set the "rotate" option in the custom view it would adjust the layout values and rotate the view so that it appears in the same location as the original layout, but is rotated so the text is vertical.

The problem I'm having is that if I create the Custom View programmatically it works fine, but if I set it up in the Designer and do the rotation in DesignerCreateView(...) it doesn't work correctly. In the designer case, the view rotates but ends up offset from where I expect it to be. It's as if it's rotating on a different pivot point.

I've checked the layout values before the rotation in both cases and they are the same, but after the rotation the view ends up in a different position,

I'm not sure if I'm doing something wrong, or if this isn't something you can do when using the designer.

I'm using the reflector library to do the rotation:
B4X:
Dim Reflect As Reflector
Reflect.Target = vw
Reflect.RunMethod2("setRotation",RotateDegrees,"java.lang.float")

Attached is a simple project that demonstrates the problem.

Any help appreciated.

Hugh
 

Attachments

  • RotateProblemDemo.zip
    44.2 KB · Views: 630

klaus

Expert
Licensed User
Longtime User
I had a look at your project, and have following comments:
1) You should move the layout definition from Activity_Resume to Activity_Create, otherwise you could get several layouts overlayed.
2) You must remove mBase.Initialize("") from the Initialize routine it is automatically initialized by the Designer.
3) You cannot use progRotatedView.GetBase in this line Activity.AddView(progRotatedView.GetBase, 140dip, 90dip, 50dip, 270dip) because there is no mBase if you have none in the Designer. In your case you have one and with progRotatedView.GetBase you get a new instance of the same object.
4) To add a custom view in the code you need to add a specific routine in your class code. I added two routines AddToParent and AddToParent2 showing two different ways to implement this.
5) I suppose that your code is not finished because you define an innerLabel Label but you dont' use it. If you add one you must also add it in the AddToParent routine.
6) I would leave the the Rotation routine more general with the rotation angle as parameter and set the angle depending on the requested orientation. I have not changed this.
7) It seems that a view added in the Designer has the pivot in the top left corner whereas a view added in the code has the pivot in the center of the view. I set the pivit to the center in the Rotate routine.

Attached a modified version of your project.
 

Attachments

  • RotateProblemDemo1.zip
    9.2 KB · Views: 677

Hugh Thomas

Member
Licensed User
Longtime User
Hi Klaus,

Thanks for taking the time to give such detailed feedback. I should have made it clear that this demo project doesn't use the actual CustomView I'm working on, it uses a basic CustomView class with just enough code to demonstrate the problem. Some of the problems you've highlight are only in this demo project and are the result of me being a bit hasty in putting it together.

I'll implement those of your suggestions that relate to the actual CustomView I'm working on.

Unfortunately, even with your corrections I've still got the same problem... if I create one custom view with designer, and one programmatically within the Main module, then after rotation they end up at different positions even though they started off in exactly the same place.

Hugh
 

Hugh Thomas

Member
Licensed User
Longtime User
My apologies Klaus, I didn't realized when I ran your updated version of my project that you'd changed the layout position of the view that's created programmatically so that it was different to the view created in the designer. So when I ran it and saw that the views ended up in different positions I thought it was because the rotation problem hadn't been fixed.

In fact you're additions to the rotation routine to set the pivot point fixed the problem.

Thanks for your help.

Hugh
 

Cableguy

Expert
Licensed User
Longtime User
hi Erel,

I'm trying to set a custom property to be float with a default value of 0.01, but the designer only shows integers...
I was hoping to get decimal incrementation
 

Cableguy

Expert
Licensed User
Longtime User
as I stated, I am casting it as float.
I can type in decimals, but using the increment/decrement arrows they only act upon the units, when I expected it to change the right most digit
 

stevel05

Expert
Licensed User
Longtime User
I understand now.
 

demasi

Active Member
Licensed User
Longtime User
Can you explain how you did to add a custom view programatically?
I'm trying to do the same but no success.
 

Cableguy

Expert
Licensed User
Longtime User
Create an "AddToParent(Parent as pane)" sub in your customview, and add the customview's base pane to the passed pane
 

LucaMs

Expert
Licensed User
Longtime User
Wonderful (perhaps I have already commented this thread, I have to look at ).

I would add these lines to the template:
B4X:
Public Sub setTag(Tag As String)
    mBase.Tag = Tag
End Sub
Public Sub getTag As String
    Return mBase.Tag
End Sub
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…