My first cross-platform app built with Java

A project to demonstrate some of the basic features and objects that can be used in TotalCross cross-platform.

Patrick Martins
TotalCross Community
11 min readMar 2, 2020

--

I’m Patrick Martins student of Control and Automation Engineering at the University of Fortaleza (Brazil). I have been a TotalCross intern since February 2020. This project was given to me to demonstrate some of the basic features and objects that can be used in TotalCross. So, if you want to learn how to create a cross-platform app with Java, stay with me!

The basic requirements for doing the steps in this article are:

  • Java 8 SDK
  • Maven
  • VS code
  • Plugin TotalCross
  • TotalCross Key

This is the guide to create your first TotalCross app to Android and Linux arm with VS Code. If you still don’t have the basic requirements installed you can go to the link with the guide to installation.

  • Download images for the implementation link.
  • For visualization of the full code, this project goes to the link.

Step 1: Creating the project

Open the VS code, press Ctrl + shift + p and search TotalCross: create a new project. Doubts about creating base file go to the link.

created project go to myfirstapp > src > main > java > com > totalcross in the class myfirstapp.java it will be where you will build the code.

Remove these lines of the project, before init next Step.

In folder myfirstapp > src > main create the resources folder and images folder inside now paste the images for this folder.

Step 2: Creating the Scroll Container

Scroll Container is a component used to add components at points that do not fit naturally on the screen, which need to go down or go to the sides.

First, you have to import the library to create the object, line 5 from the code below, these steps were repeated for all objects that will be used, then name and define your initial variables in the constructor method, inline 21 in the code below were set to false and true, thus disabling the horizontal scroll. If you want the scroll to be possible for both situations you must keep the method empty.

Next, you have to add the screen using the add method, which has to be configured with the object to be inserted on the screen and the position variables respectively, object, horizontal position, vertical position, width, height, as shown on line 22.

Code

With the created container you can add the objects in it using the add method as follows:

Step 3: ADD images

Two images will be added, one to be the background of the app and the other one logo. how the previous object must add the library before it can be used.

Then he had created the objects to receive the images.

You should now import the image using the builder.

To be able to change the image parameters, it is necessary to use another ImageControl object adding its library.

Instantiating objects.

defining its characteristics.

add in the container.

The complete code should look like this at that point.

Code

Application

Step 4: ADD Label

Labels are the object you can use to add text to the application.

Two labels will be used, one to title the application and the other to title a questionnaire.

As the background is a predominantly red image, it is necessary to change the color of some objects, for this, you can use the Color library.

Code

Application

Step 5: ADD Edit

this an input component used to receive text and date, it has a method that allows you to receive dates by default, setMode must have as input Edit.DATE, true for that to be possible.

Code

Application

Check is an input with a checkbox that indicates when it was clicked. In this case, it will be used so that the user can define his gender in the form. For that, it will be necessary to use 3 objects.

When clicked, the check generates a wave that propagates from the clicked point to fill the entire object, in this case, this damages the aesthetics, to remove the effect method must be null.

When you add PREFERRED in the add method, the object’s borders will be the size that accommodates only the checkbox and the text.

Code

Application

Step 7: Add Button

The buttons will be used to complete the registration or to clear the added information.

The colors of the buttons will be changed to contrast with the red background.

Full Code

Application

Conclusion

Now with the finished project, a cross-platform app built with Java, we have a front end of forms with some of the most used objects, with that it is possible to see the functionality and the use of these, from that point on the functionality of the app is in charge of the developer.

--

--