Android Tutorial [java] Custom View with Designer Support (Java library)

Informatix

Expert
Licensed User
Longtime User
What about the solution proposed above? Does it answer the concern about the panel?

I still cannot move the view with setLeft and setTop, and I don't understand why.

What does @Pixel?

This is a bit of a hack, but you can let the user write the cache size in the Tag field (to override a reasonable default value).

??? Sorry I don't understand. If I have two or three parameters, what am I supposed to do?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
How did you implement these methods? The simplest way is to use the base panel. Without it you can use code similar to:
B4X:
   /**
    * Gets or sets the view's left position.
    */
   public int getLeft() {
      BALayout.LayoutParams lp = (LayoutParams) getObject().getLayoutParams();
      return lp.left;
   }
   public void setLeft(@Pixel int left) {
      BALayout.LayoutParams lp = (LayoutParams) getObject().getLayoutParams();
      lp.left = left;
      getObject().getParent().requestLayout();
   }
You should replace getObject with the actual view.

@Pixel is a new annotation. It tells the compiler that this value is a "dimension value" and is expected to be scaled. It only affects the new warning engine.

Sorry I don't understand. If I have two or three parameters, what am I supposed to do?
One option is to let the user set the other parameters programmatically. If these parameters must be set before the view is created then for now you cannot use this feature.
 

Informatix

Expert
Licensed User
Longtime User
Without it you can use code similar to:

Yes, I finally did a copy/paste from one of my libraries. I didn't remember that we cannot use setLeft and setTop.
I attached the modified version (without the base panel).

If these parameters must be set before the view is created then for now you cannot use this feature.

Ok. But, frankly speaking, I'm a bit surprised by the choices you made. Why do you limit so much your designer? Why do you create such constraints? A designer is not really complex (in the past, I programmed myself two designers), so for someone as skilled as you are, I can't imagine there are any difficulties. Or maybe it's just a choice, but as you understood, IMHO it's a bad choice. Anyway, if you're short on time and you need someone to help you to improve your designer, you know that you can count on me (and probably a few other guys that read this thread).
 

Attachments

  • SwitchTest.zip
    14.2 KB · Views: 875

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is certainly possible that new options will be added in the future. The designer component is not a "standalone" component. Adding a custom property depends on support for custom properties in the doclet tool, the IDE, classes and so on.

It is not too difficult however it is something I prefer to add in a later version (when we all better understand the requirements).
One of the "problems" with a tool such as Basic4android is that you can only add features. It is almost impossible to modify or remove features without breaking backwards compatibility. For that reason I prefer to start simple and then extend the features when things are more clear.
 

Informatix

Expert
Licensed User
Longtime User
It is certainly possible that new options will be added in the future. The designer component is not a "standalone" component. Adding a custom property depends on support for custom properties in the doclet tool, the IDE, classes and so on.

It is not too difficult however it is something I prefer to add in a later version (when we all better understand the requirements).
One of the "problems" with a tool such as Basic4android is that you can only add features. It is almost impossible to modify or remove features without breaking backwards compatibility. For that reason I prefer to start simple and then extend the features when things are more clear.

Thank you for the explanation.
 

pappicio

Active Member
Licensed User
Longtime User
I don't understand, reading, if the customview will be automatically seen into designer.... if not.... it'll be fantastic to have:
I create a jar file, like external lib, but if inserted into a custom folder, and imported into b4a, like the external libs, each external customview, could be visible into addviews designer's menu!!!! could it will be possible? how yet is possible to build custom controls and insert them into designer object menu into visual studio .NET, for example!
thanks!
 

woniol

Active Member
Licensed User
Longtime User
Informatix , could You please give us another example od Switch library with adding switch to Activity or panel without using Designer.
Could You explain usage of
MySwitch.DesignerCreateView
MySwitch.Initialize()
MySwitch.AddToParent()

and it's parameters.
 

woniol

Active Member
Licensed User
Longtime User
Oh, i understand...
would it be a lot of work to create 'full' B4A library with this switch class ? :)
 

westingenieria

Active Member
Licensed User
Longtime User
Oh I agree. Performance is not what bothers me. It's the use.
I attached to this post the Switch library (copied from post #1, with two properties added: Left and Top) and a test project.
My steps:
1) I create a layout with the switch in the middle.
2) I open the layout with LoadLayout. Ok, my switch is loaded and it appears where I placed it.
3) I set its left and top properties to move it to the top left corner, but it does not move. It's logical: it's embedded inside a panel, so its coordinates are not relative to the activity but to this panel. I have to move the panel (cumbersome!). The problem is that I have no access to this panel. And getParent, with the Reflection library, raises an error.

So how can I move the view dynamically?

hi, how i can change color of switch to blue?

thanks
 

daemon

Active Member
Licensed User
Longtime User
Hi,

I'm following this tutorial to write a wrapper for crosswalk webview, which gives following two constructors:
public XWalkView(android.content.Context context, android.util.AttributeSet attrs);
public XWalkView(android.content.Context context, android.app.Activity activity);

I see that initialize function of DesignerCustomView looks like:
public void _initialize(BA ba, Object activityClass, String EventName)

I assume activityClass will translate to activity (2nd parameter of XWalkView constructor), but I'm not sure how to map / type-cast / convert / translate it.

Any help will be useful.
 

daemon

Active Member
Licensed User
Longtime User
Even for that I'll need to pass activity as second parameter to constructor. I tried using 'Me' or 'Activity' but that gives compile error.

How do I pass reference to Main activity?
 
Last edited:

daemon

Active Member
Licensed User
Longtime User
I created a custom view following this guide and I'm able to use it with designer. I can also see all methods / properties working as expected.

However, designer has some attributes e.g. Visible, which I haven't explicitely defined and I do not see them in autocompletion. So, I cannot change visibility of my view programmatically.

How do I enable changing of these attributes programatically?
I guess I have to extend an already available class?
 
Top