B4X Booklets
B4X JavaObject NativeObject
Copyright: © 2024 Anywhere Software Edition 2.5
Last update: 2024.01.05
Table of contents 2 B4X JavaObject NativeObject
1 B4X platforms .............................................................................................................................. 5
2 General ......................................................................................................................................... 6
3 B4A JavaObject .......................................................................................................................... 7
3.1 B4A JavaObject methods ..................................................................................................... 8
3.1.1 CreateEvent ...................................................................................................................... 8
3.1.2 CreateEventFromUI ......................................................................................................... 9
3.1.3 GetField ............................................................................................................................ 9
3.1.4 GetFieldJO ....................................................................................................................... 9
3.1.5 InitializeArray .................................................................................................................. 9
3.1.6 InitializeContext ............................................................................................................... 9
3.1.7 InitializeNewInstance ..................................................................................................... 10
3.1.8 InitializeStatic ................................................................................................................ 10
3.1.9 IsInitialized ..................................................................................................................... 10
3.1.10 RunMethod ................................................................................................................. 11
3.1.11 RunMethodJO ............................................................................................................ 12
3.1.12 SetField ...................................................................................................................... 12
3.2 How to use it ...................................................................................................................... 13
3.3 B4A examples .................................................................................................................... 16
3.3.1 Get B4A Context ............................................................................................................ 16
3.3.1.1 GetB4A .................................................................................................................. 16
3.3.1.2 getContext .............................................................................................................. 16
3.3.2 ViewUtils ....................................................................................................................... 17
3.3.2.1 getParent ................................................................................................................. 17
3.3.2.2 getRootView .......................................................................................................... 17
3.3.2.3 setRotation ............................................................................................................. 17
3.3.2.4 setRotationX ........................................................................................................... 17
3.3.2.5 setRotationY ........................................................................................................... 18
3.3.2.6 setPivotX ................................................................................................................ 18
3.3.2.7 getPivotX ............................................................................................................... 18
3.3.2.8 setPivotY ................................................................................................................ 18
3.3.2.9 getPivotY ............................................................................................................... 18
3.3.3 EditText Utils ................................................................................................................. 19
3.3.3.1 setCursorVisible ..................................................................................................... 19
3.3.3.2 setTextIsSelectable ................................................................................................. 19
3.3.3.3 setSelection ............................................................................................................ 19
3.3.3.4 getSelectionStart .................................................................................................... 19
3.3.3.5 getSelectionEnd...................................................................................................... 19
3.3.3.6 getLineHeight ......................................................................................................... 20
3.3.3.7 getLineCount .......................................................................................................... 20
3.3.3.8 getHighLightColor / setHighLightColor ................................................................ 20
3.3.3.9 GetXYCursor ......................................................................................................... 21
3.3.4 Label Utils ...................................................................................................................... 22
3.3.4.1 setEllipsize ............................................................................................................. 22
3.3.4.2 resetEllipsize .......................................................................................................... 22
3.3.4.3 setSingleLIne.......................................................................................................... 22
3.3.4.4 setLines .................................................................................................................. 23
3.3.4.5 setMarqueeRepeat .................................................................................................. 23
3.3.4.6 Stop or run the horizontal scrolling ........................................................................ 23
3.3.4.7 setShadowLayer ..................................................................................................... 23
4 B4J JavaObject ........................................................................................................................... 24
4.1 B4J JavaObjec Methods ..................................................................................................... 24
4.1.1 CreateEvent .................................................................................................................... 24
4.1.2 CreateEventFromUI ....................................................................................................... 26
Table of contents 3 B4X JavaObject NativeObject
4.1.3 GetField .......................................................................................................................... 26
4.1.4 GetFieldJO ..................................................................................................................... 26
4.1.5 InitializeArray ................................................................................................................ 26
4.1.6 InitializeContext ............................................................................................................. 26
4.1.7 InitializeNewInstance ..................................................................................................... 27
4.1.8 InitializeStatic ................................................................................................................ 27
4.1.9 IsInitialized ..................................................................................................................... 27
4.1.10 RunMethod ................................................................................................................. 28
4.1.11 RunMethodJO ............................................................................................................ 29
4.1.12 SetField ...................................................................................................................... 29
4.2 How to use it ...................................................................................................................... 30
4.3 B4J examples ..................................................................................................................... 31
4.3.1 setMouseTransparent ..................................................................................................... 31
4.3.2 setTextAlignment ........................................................................................................... 31
4.3.3 Graphics jCanvasExt ...................................................................................................... 32
4.3.4 GoogleMaps ................................................................................................................... 33
5 B4i NativeObject ........................................................................................................................ 34
5.1 B4i NativeObject methods ................................................................................................. 34
5.1.1 ArrayFromEdgeInsets .................................................................................................... 34
5.1.2 ArrayFromPoint ............................................................................................................. 34
5.1.3 ArrayFromRange............................................................................................................ 34
5.1.4 ArrayFromRect .............................................................................................................. 34
5.1.5 ArrayFromSize ............................................................................................................... 35
5.1.6 ArrayToNSData ............................................................................................................. 35
5.1.7 AsBoolean ...................................................................................................................... 35
5.1.8 AsNumber ...................................................................................................................... 35
5.1.9 AsString ......................................................................................................................... 35
5.1.10 ColorToUIColor ......................................................................................................... 35
5.1.11 CreateBlock ................................................................................................................ 36
5.1.12 GetField ...................................................................................................................... 36
5.1.13 Initialize( .................................................................................................................... 36
5.1.14 IsInitialized ................................................................................................................. 36
5.1.15 MakeEdgeInsets ......................................................................................................... 36
5.1.16 MakePoint .................................................................................................................. 37
5.1.17 MakeRange ................................................................................................................ 37
5.1.18 MakeRect ................................................................................................................... 37
5.1.19 MakeSize .................................................................................................................... 37
5.1.20 NSDataToArray ......................................................................................................... 37
5.1.21 RunMethod ................................................................................................................. 38
5.1.22 RunMethodWithBlocks ............................................................................................. 38
5.1.23 SetField ...................................................................................................................... 38
5.1.24 Tag.............................................................................................................................. 38
5.1.25 UIColorToColor ......................................................................................................... 38
5.2 How to use it ...................................................................................................................... 39
5.3 B4i examples ...................................................................................................................... 40
5.3.1 SetButtonTextColor ....................................................................................................... 40
5.3.2 Hide the StatusBar.......................................................................................................... 40
5.3.3 Setting the status bar to use a light theme ...................................................................... 40
5.3.4 Check StatusBar state ..................................................................................................... 40
5.3.5 Get Border properties ..................................................................................................... 41
5.3.6 Getting current device language..................................................................................... 41
5.3.7 Set the NavigationBar text color .................................................................................... 41
Table of contents 4 B4X JavaObject NativeObject
Main contributors: Klaus Christl (klaus), Erel Uziel (Erel)
To search for a given word or sentence use the Search function in the Edit menu.
All the source code and files needed (layouts, images etc.) of the example projects in this booklet
are included in the SourceCode folder.
Updated for:
B4A version 12.80
B4i version 8.50
B4J version 10.00
B4X Booklets:
B4X Getting Started
B4X Language
B4X IDE Integrated Development Environment
B4X Visual Designer
B4X Help tools
B4XPages Cross-platform projects
B4X CustomViews
B4X Graphics
B4X XUI B4X User Interface
B4X SQLite Database
B4X JavaObject NativeObject
B4R Example Projects
You can consult these booklets online in this link [B4X] Documentation Booklets.
Be aware that external links don’t work in the online display.
1 B4X platforms 5 B4X JavaObject NativeObject XUI
1 B4X platforms
B4X is a suite of programming languages for different platforms.
B4X suite supports more platforms than any other tool
ANDROID | IOS | WINDOWS | MAC | LINUX | ARDUINO | RASPBERRY PI | ESP8266 | AND
MORE...
B4A Android
B4A is a 100% free development tool for Android applications, it includes all the features
needed to quickly develop any type of Android app.
B4i iOS
B4i is a development tool for native iOS applications.
B4i follows the same concepts as B4A, allowing you to reuse most of the code and build
apps for both Android and iOS.
B4J Java / Windows / Mac / Linux / Raspberry PI
B4J is a 100% free development tool for desktop, server and IoT solutions.
With B4J you can easily create desktop applications (UI), console programs (non-UI) and
server solutions.
The compiled apps can run on Windows, Mac, Linux and ARM boards (such as Raspberry
Pi).
B4R Arduino / ESP8266
B4R is a 100% free development tool for native Arduino and ESP8266 programs.
B4R follows the same concepts of the other B4X tools, providing a simple and powerful
development tool.
B4R, B4A, B4J and B4i together make the best development solution for the Internet of
Things (IoT).
B4XPages
B4XPages is an internal library for B4A, B4i and B4J allowing to develop easily cross-
platform programs.
B4XPages is explained in detail in the B4XPages Cross-platform projects booklet.
Even, if you want to develop only in one platform it is interesting to use the B4XPages
library it makes the program flow simpler especially for B4A.
2 General 6 B4X JavaObject NativeObject
2 General
JavaObject can be used to access Android or Java objects, or properties not directly exposed to B4A
or B4J.
The library is the same for B4A and B4J.
NativeObject is like JavaObject but for B4i.
To use JavaObject or NativeObject you should know or learn how the operating systems are
constructed and how they work.
The purpose of this booklet is to give an ‘entry point’ with examples, to better understand how to
use it.
It is not an encyclopedia of routines.
3 B4A JavaObject 7 B4X JavaObject NativeObject
3 B4A JavaObject
The purpose of JavaObject library is similar to the purpose of Reflection library. Both libraries
allow you to directly call Java APIs based on Java reflection features.
JavaObject design is different than Reflection library and, in most cases, simpler to use. However,
JavaObject doesn't replace Reflection library as it doesn't support all its features. In many cases you
can use both libraries together (both are lightweight libraries).
JavaObject approach is more "object oriented". You declare a JavaObject object which then
"wraps" any other object and provide three methods: SetField, GetField and RunMethod.
JavaObject variable is similar to an Object variable with the addition of the reflection methods.
Notes
JavaObject can only access public methods and fields (unlike Reflection library).
JavaObject doesn't include the helper methods to access the context and other fields as in
Reflection library. You can use both libraries together when these fields are needed.
There is almost no overhead for a JavaObject instance. It is better to create multiple
JavaObjects instead of reusing a single instance.
3.1 B4A JavaObject methods 8 B4X JavaObject NativeObject
3.1 B4A JavaObject methods
3.1.1 CreateEvent
CreateEvent (Interface As String, EventName As String, DefaultReturnValue As Object)
Creates an instance of the interface and binds it to the object.
Interface - The full interface name.
EventName - The prefix of the event sub.
DefaultReturnValue - This value will be returned if no value was returned from the event sub. This
can happen if the Activity is paused for example.
B4A example:
Sub Activity_Create(FirstTime As Boolean)
Dim btn As Button
btn.Initialize("")
Activity.AddView(btn, 0, 0, 200dip, 200dip)
Dim jo As JavaObject = btn
Dim e As Object = jo.CreateEvent("android.view.View.OnTouchListener", "btnTouch",
False)
jo.RunMethod("setOnTouchListener", Array As Object(e))
End Sub
And the event routine:
Sub btnTouch_Event (MethodName As String, Args() As Object) As Object
Dim MotionEvent As JavaObject = Args(1) 'args(0) is View
Dim x As Float = MotionEvent.RunMethod("getX", Null)
Dim y As Float = MotionEvent.RunMethod("getY", Null)
Log(x & ", " & y)
Dim Action As Int = MotionEvent.RunMethod("getAction", Null)
Log("Action = " & Action)
Return True
End Sub
And the Android documentation: View.OnTouchListener, MotionEvent.
If you look at the documentation, you may notice that there is a lot to learn.
3.1 B4A JavaObject methods 9 B4X JavaObject NativeObject
3.1.2 CreateEventFromUI
CreateEventFromUI (Interface As String, EventName As String, ReturnValue As Object)
Similar to CreateEvent. The event will be sent to the message queue and then be processed (similar
to CallSubDelayed).
3.1.3 GetField
GetField (Field As String)
Gets the value of the given field.
3.1.4 GetFieldJO
GetFieldJO (Field As String)
Similar to GetField. Returns a JavaObject instead of Object.
Returns a JavaObject.
3.1.5 InitializeArray
InitializeArray (ClassName As String, Values As Object())
Creates an array with the given class and values.
Returns a JavaObject.
3.1.6 InitializeContext
B4A only method.
Initializes the object with the current context (current Activity or Service).
Returns a JavaObject.
3.1 B4A JavaObject methods 10 B4X JavaObject NativeObject
3.1.7 InitializeNewInstance
InitializeNewInstance (ClassName As String, Params As Object())
Creates a new instance of the given class.
ClassName - The full class name.
Params - An array of objects to pass to the constructor (or Null).
Returns a JavaObject.
3.1.8 InitializeStatic
InitializeStatic (ClassName As String)
Initializes the object. The object will wrap the given class (for static access).
ClassName - The full class name.
Returns a JavaObject.
3.1.9 IsInitialized
Returns a Boolean.