Using custom classes

Discussion in 'Designer4android Tutorials' started by Erel, Apr 19, 2012.

  1. Erel

    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:
    Code:
    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:
    [​IMG]

    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.
     
  2. Amalkotey

    Amalkotey Active Member Licensed User

    Is it a future in the next B4A version? In B4A 1.90, I cannot find NativeClass.
     
  3. barx

    barx Well-Known Member Licensed User

    This is for Designer4Android. It is a separate product to Basic4Android
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice