Android Tutorial [B4X] Class templates

*** This is a trick, a discovery by @aeric, really very useful ***

[B4A-B4J-B4i]


[This is a long post but the process is much simpler than it seems].


As you know, you can create your own project templates:
https://www.b4x.com/android/forum/threads/b4x-projects-templates.119901/#content

Creating your own class templates to add to your projects is an unknown feature, as far as I know. @aeric managed to do it!


In this image you can see which class templates you can add to a B4J (or B4A/B4i) project:
1758253974993.png



but here you can see that I now have two more class templates: "Aeric's B4XPage" and "Aeric's Sample Class":
1758254450400.png


You might have your own class templates in that list.

But how is that possible?

  1. Prepare your class templates; for example, you might have your own B4XPage template for log in/sign in (MyLoginPage.bas) and a class containing your utility functions (MyUtils.bas).
  2. Change the extensions of the two .bas files to .b4x_excluded:
    MyLoginPage.b4x_excluded and MyUtils.b4x_excluded
  3. Create a .b4xlib library. It's very simple: create a zip file (for example, "MyClassTemplates.zip"), add the two classes and a text file, "manifest.txt" to it. Change the extension of this zip file: MyClassTemplates.b4xlib, which now contains:

    1758256663961.png
  4. The manifest.txt must contain the statement CustomClassTemplates.
    The format of CustomClassTemplates is as follows:
    CustomClassTemplates=<Menu class name>|<Class name (without extension) >, ... (here other <Menu class name>|<Class name (without extension) >).
    In this example it could be:
    CustomClassTemplates=lm Login B4XPage|MyLoginPage,lm Utils|MyUtils
    Add to the manifest file everything that this type of file can normally contain. In particular, indicate the type of projects supported:
    Supported Platforms=B4A,B4i,B4J
    or
    Supported Platforms=B4A
    etc.

(I chose terrible names, I hope this is clear anyway).

Once this is done, place the library in the additional libraries folders, depending on the type of project you support. For example, if the classes work on all three "platforms," place the library in the B4X additional libraries folder.

Import (select) the library (MyClassTemplates.b4xlib) into your project and yours will be among the class templates you can add.


1758258756833.png


Now I could add a new "lmLoginB4XPage" or "lmUtils" class, not only "Standard Class"...
1758259291594.png



Another way to add a new class (here is B4J - the same in B4A/B4i): mouse right click on the tab Modules:
1758259445630.png



Once you have added the classes to your project, you can (should) remove your library (in this example: MyClassTemplates).
 

Attachments

  • 1758258608074.png
    1758258608074.png
    4.4 KB · Views: 16
Last edited:

LucaMs

Expert
Licensed User
Longtime User
Creating your own class templates to add to your projects is an unknown feature, as far as I know. @aeric managed to do it!

While creating the previous post, I noticed I had an "MyCustomClasses" folder in the additional B4A libraries folder.
It contains some .b4x_excluded files, and these are listed among the addable classes.

1758260188284.png


Simply forgot it! :confused:

@aeric's way, that is creating b4xlibs, is still very useful, more convenient.
 

aeric

Expert
Licensed User
Longtime User
Once you have added the classes to your project, you can (should) remove your library (in this example: MyClassTemplates).
No, it won't included in the compiled project. As the file extension said, "b4x_excluded".
So it is safe to leave the library checked in Libraries Manager.
Note that the library will not add anything to the compiled project.
 

Theera

Expert
Licensed User
Longtime User
Hi LacaMs
Is there example files for test adding? I'm not clearly if I don't tried once.
 

Theera

Expert
Licensed User
Longtime User
Top