Using custom classes

Erel

Administrator
Staff member
Licensed User
In many cases you will need to extend (subclass) an existing view.
The NativeClass property allows you to override the default type with a custom one. This property doesn't have any effect during design. Only when the layout is loaded.

For example if we want to create a specialized EditText that shows a toast message when the selection area is changed we can create the following class:
B4X:
   public static class MyEditText extends EditText {
       public MyEditText(Context context) {
         super(context);
      }
      protected void onSelectionChanged(int selStart, int selEnd) {
         if (selEnd - selStart > 0)
            Toast.makeText(getContext(), getText().subSequence(selStart, selEnd), Toast.LENGTH_SHORT).show();
       }
   }
Assuming that this class is declared inside an activity named Test1Activity we should set the NativeClass property to:


Note that if the property starts with a dot then the application package is automatically added.

The layout builder expects a constructor with a single argument which is the context.

Tips:
- Label is based on TextView. You can use label with any class that extends TextView.
- Panel (without any children) supports all classes that extend View.
 

barx

Well-Known Member
Licensed User
This is for Designer4Android. It is a separate product to Basic4Android
 
Top