ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / Developing for Android
  • Overview
  • How to create a procedure in Kotlin?
  • Writing a global procedure in Kotlin
  • Important notes
  • Generating the application
  • How to execute a WLanguage procedure?
  • Executing a WLanguage procedure from Kotlin code
  • Important notes
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac CatalystUniversal Windows 10 App
Others
Stored procedures
Overview
Kotlin is a programming language mainly used for Android development.
Starting with version 28, you can write Kotlin code in the code editor directly.
Syntax highlighting makes it easier to write and read code in this language.
This allows you to create global procedures entirely in Kotlin.
These Kotlin procedures can call directly WLanguage procedures in your project.
How to create a procedure in Kotlin?

Writing a global procedure in Kotlin

To write a global procedure in Kotlin:
  1. Create a new global procedure (via "New global procedure" in the context menu of sets of procedures in the "Project explorer" pane). Specify the name of this procedure.
  2. The procedure is created in the code editor.
  3. In the header of the procedure, click "WL" to change the type of code.
    Types of code
  4. In the list that opens, select "Kotlin".
  5. The header becomes purple and "Kotlin" appears to the left of the procedure name. The existing code is commented out.
    Procedure in Kotlin mode
  6. Write the Kotlin code directly in the code editor in WINDEV Mobile.
Remark: To switch back to WLanguage code, simply click "Kotlin" in the procedure header and select "WLanguage".

Important notes

  • In Kotlin procedures, parameters are automatically passed by value.
  • The parameters of procedures written in Kotlin must be of a primitive type (integer, real, string, etc.).
    Caution: The float type is not supported, it can be replaced with the double type.
  • The return values of methods must be of a primitive type (integer, real, string, etc.).
    Caution: The float type is not supported, it can be replaced with the double type.
  • Caution: you need to import the classes used in the native Kotlin code with the "Import" keyword. It is recommended to import these elements before declaring the method.
  • Kotlin is case-sensitive: make sure you always use the same lowercase and uppercase characters in the function names.
  • If the Kotlin code entered requires specific permissions, they must be explicitly defined when generating the application. For more details, see Generating an Android application.
  • The classes and methods of Android SDK often handle a reference on a "Context" object. In an Android project, when writing native code, you can retrieve a "Context" object by referencing the current activity or the application context via the following methods (these methods can be directly called in the code of the procedure):
    // Get the current activity
    getCurrentActivity()

    // Get the current "Context"
    getApplicationContext()
  • To retrieve the View object corresponding to the main control component, you must use the getView method while passing the control name.
    Example: The following code returns the instance of the "Button" class used by the control:
    getView("BTN_Button1")

Generating the application

The Kotlin classes used in the native Kotlin code must be specified in the Android application generation wizard:
  • "Integrate libraries" step for classes included in ".jar" or ".aar" libraries.
  • "Integrate Maven dependencies" step for Maven dependencies.
    Remark: These classes cannot be tested via the GO option. The application must be generated and launched.
How to execute a WLanguage procedure?

Executing a WLanguage procedure from Kotlin code

To execute a WLanguage procedure from the Kotlin code, simply call one of the following methods depending on the type of the value returned by the procedure:
Method nameReturn Kotlin type
callWLProcedurevoid (no return value)
callWLProcedure_StringString
callWLProcedure_intint
callWLProcedure_doubledouble
callWLProcedure_longlong
callWLProcedure_booleanboolean


In the call to the selected method:
  • the first parameter the method takes is the name of the WLanguage procedure to be executed.
  • the other parameters correspond to the parameters expected by the WLanguage procedure (if necessary). You can use parameters of any simple Kotlin type (string, int, double, long, boolean, etc.). If the specified parameter does not correspond to a simple Kotlin type, it will be automatically converted to "string" by calling its "toString()" method.
Example:
Code of WLanguage procedure:
GLOBAL procédure MyProcedure(n is int, b is boolean, s is string)
...
 
RESULT s
To call MyProcedure from the Kotlin code, use:
callWLProcedure_String("MyProcedure",5,false,"test")

Important notes

  • If the procedure to run is a local procedure, we advise you to give its full name by specifying the name of the element to which the procedure belongs. Example: "WIN_Window1.MyProcedure".
  • The name of the procedure to be executed must contain only unaccented alphanumeric characters.
  • If the procedure to be executed returns a value, this value will be automatically converted to the primitive Kotlin type corresponding to the method used to call the procedure. If the conversion fails, a fatal error occurs.
  • The parameters of the procedure are passed by value.
Minimum version required
  • Version 28
Comments
Click [Add] to post a comment

Last update: 03/31/2023

Send a report | Local help