B4xJavaObject NativeObjectV1 7
B4xJavaObject NativeObjectV1 7
B4xJavaObject NativeObjectV1 7
1 B4X .............................................................................................................................................. 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 GetFielJO ......................................................................................................................... 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.4 Label Utils ...................................................................................................................... 20
3.3.4.1 setEllipsize ............................................................................................................. 20
3.3.4.2 resetEllipsize .......................................................................................................... 20
3.3.4.3 setSingleLIne.......................................................................................................... 20
3.3.4.4 setLines .................................................................................................................. 21
3.3.4.5 setMarqueeRepeat .................................................................................................. 21
3.3.4.6 Stop or run the horizontal scrolling........................................................................ 21
3.3.4.7 setShadowLayer ..................................................................................................... 21
4 B4J JavaObject ........................................................................................................................... 22
4.1 B4J JavaObjec Methods ..................................................................................................... 22
4.1.1 CreateEvent .................................................................................................................... 22
4.1.2 CreateEventFromUI ....................................................................................................... 24
4.1.3 GetField .......................................................................................................................... 24
4.1.4 GetFielJO ....................................................................................................................... 24
4.1.5 InitializeArray ................................................................................................................ 24
4.1.6 InitializeContext ............................................................................................................. 24
Table of contents 3 B4X JavaObject NativeObject
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 9.80
B4i version 6.30
B4J version 8.10
B4X Booklets:
B4X Getting Started
B4X Basic Language
B4X IDE Integrated Development Environment
B4X Visual Designer
B4X CustomViews
B4X Graphics
B4X XUI B4X User Interface
B4X SQLite Database
B4X JavaObject NativeObject
1 B4X
B4X is a suite of BASIC programming languages for different platforms.
• 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
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 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).
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.
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.
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 of 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.1 CreateEvent
B4A example:
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
Similar to CreateEvent. The event will be sent to the message queue and then be processed (similar
to CallSubDelayed).
3.1.3 GetField
3.1.4 GetFielJO
Returns a JavaObject.
3.1.5 InitializeArray
Returns a JavaObject.
3.1.6 InitializeContext
Returns a JavaObject.
3.1 B4A JavaObject methods 10 B4X JavaObject NativeObject
3.1.7 InitializeNewInstance
Returns a JavaObject.
3.1.8 InitializeStatic
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.
3.1 B4A JavaObject methods 11 B4X JavaObject NativeObject
3.1.10 RunMethod
Runs the given method and returns the method return value.
MethodName - The case-sensitive method name.
Params - Method parameters (or Null).
Returns an Object.
B4A example:
Get and sets the Labels padding.
3.1.11 RunMethodJO
Returns a JavaObject.
3.1.12 SetField
Returns Void.
3.2 B4A JavaObject How to use it 13 B4X JavaObject NativeObject
Let’s add the Ellipsize property to a view. This property is implemented in B4A, but not the
MARQUEE mode which allows scrolling the text in the TextView.
Click on:
Below this title you find all public methods of the TextView.
It is a huge ‘chapter’!
3.2 B4A JavaObject How to use it 14 B4X JavaObject NativeObject
3.3.1.1 GetB4A
3.3.1.2 getContext
3.3.2 ViewUtils
3.3.2.1 getParent
3.3.2.2 getRootView
'Returns the root view of the given view, it's mainly the Activity
Sub getRootView(v As View) As View
Dim jo = v As JavaObject
Return jo.RunMethod("getRootView", Null)
End Sub
3.3.2.3 setRotation
3.3.2.4 setRotationX
'Sets the rotation angle around the X axis of the given view
'v = view
'Angle = rotation angle in degrees
Sub setRotationX(v As View, Angle As Float)
Dim jo = v As JavaObject
jo.RunMethod("setRotationX", Array As Object(Angle))
End Sub
3.3 B4A examples 18 B4X JavaObject NativeObject
3.3.2.5 setRotationY
'Sets the rotation angle around the Y axis of the given view
'v = view
'Angle = rotation angle in degrees
Sub setRotationY(v As View, Angle As Float)
Dim jo = v As JavaObject
jo.RunMethod("setRotationY", Array As Object(Angle))
End Sub
3.3.2.6 setPivotX
3.3.2.7 getPivotX
3.3.2.8 setPivotY
3.3.2.9 getPivotY
These methods concern EditText views and are in the forum EditText Utils.
All the methods below can be used without a sub like:
Dim jo = edt As JavaObject
3.3.3.1 setCursorVisible
3.3.3.2 setTextIsSelectable
3.3.3.3 setSelection
3.3.3.4 getSelectionStart
3.3.3.5 getSelectionEnd
3.3.4.1 setEllipsize
3.3.4.2 resetEllipsize
3.3.4.3 setSingleLIne
3.3.4.4 setLines
3.3.4.5 setMarqueeRepeat
3.3.4.7 setShadowLayer
4 B4J JavaObject
4.1.1 CreateEvent
B4A example:
If you look at the documentation, you may notice that there is a lot to learn.
4 B4J JavaObject 24 B4X JavaObject NativeObject
4.1.2 CreateEventFromUI
Similar to CreateEvent. The event will be sent to the message queue and then be processed (similar
to CallSubDelayed).
4.1.3 GetField
4.1.4 GetFielJO
Returns a JavaObject.
4.1.5 InitializeArray
Returns a JavaObject.
4.1.6 InitializeContext
Returns a JavaObject.
4 B4J JavaObject 25 B4X JavaObject NativeObject
4.1.7 InitializeNewInstance
Returns a JavaObject.
4.1.8 InitializeStatic
Initializes the object. The object will wrap the given class (for static access).
ClassName - The full class name.
Returns a JavaObject.
4.1.9 IsInitialized
Returns a Boolean.
4 B4J JavaObject 26 B4X JavaObject NativeObject
4.1.10 RunMethod
Runs the given method and returns the method return value.
MethodName - The case-sensitive method name.
Params - Method parameters (or Null).
Returns an Object.
B4A example:
Get and sets the Labels padding.
4.1.11 RunMethodJO
Returns a JavaObject.
4.1.12 SetField
Returns Void.
4.2 B4J JavaObject How to use it 28 B4X JavaObject NativeObject
On the top left you find all the Packages, and just below, all classes:
4.3 B4J examples 29 B4X JavaObject NativeObject
https://www.b4x.com/android/forum/threads/google-map-with-custom-style.84894/#content
https://www.b4x.com/android/forum/threads/jgauges.70538/#content
https://www.b4x.com/android/forum/threads/excel-charts-and-templates-with-jpoi-
library.57689/#content
https://www.b4x.com/android/forum/threads/class-textflow-similar-to-b4a-b4i-
richstring.61237/#content
https://www.b4x.com/android/forum/threads/jcanvasext-extends-canvas-methods.86407/#content
https://www.b4x.com/android/forum/threads/using-javaobject-and-javafx-linechart.34777/#content
4.3.1 setMouseTransparent
In B4J, when a node covers other nodes, like a Pane, the mouse events are not submitted to
underlying nodes.
This is the same as in B4i, it is inverse in B4A.
In B4i, you can use the UserInteractionEnabled property.
In B4J you can use the code below to enable mouse events beeing submitted to underlying nodes.
4.3.2 setTextAlignment
The standard Alignment property does not work properly with multiline labels. It aligns the content
based on the first line.
You can use this code instead:
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/canvas/GraphicsContext.html
4.3 B4J examples 31 B4X JavaObject NativeObject
4.3.4 GoogleMaps
https://www.b4x.com/android/forum/threads/googlemapsdemo.91906/#content
https://www.b4x.com/android/forum/threads/class-googlemapsextra.56871/#content
5 B4i NativeObject 32 B4X JavaObject NativeObject
5 B4i NativeObject
5.1.1 ArrayFromEdgeInsets
Converts a UIEdgeInsets to an array with the Left, Top, Right and Bottom values.
Returns: Float()
5.1.2 ArrayFromPoint
ArrayFromPoint(Point As NSData*)
Returns: Float()
5.1.3 ArrayFromRange
Returns: Int()
5.1.4 ArrayFromRect
Returns: Float()
5 B4i NativeObject 33 B4X JavaObject NativeObject
5.1.5 ArrayFromSize
Returns: Float()
5.1.6 ArrayToNSData
Returns : NSObject
5.1.7 AsBoolean
Returns : BOOL
5.1.8 AsNumber
Returns: NSObject
5.1.9 AsString
Returns: NSString
5.1.10 ColorToUIColor
Returns: UIColor
5 B4i NativeObject 34 B4X JavaObject NativeObject
5.1.11 CreateBlock
Returns: Id
5.1.12 GetField
Returns: B4INativeObject
5.1.13 Initialize(
Returns: B4INativeObject
5.1.14 IsInitialized
Returns: BOOL
5.1.15 MakeEdgeInsets
Returns: NSData
5 B4i NativeObject 35 B4X JavaObject NativeObject
5.1.16 MakePoint
Returns: NSData
5.1.17 MakeRange
Returns: NSData
5.1.18 MakeRect
Returns: NSData
5.1.19 MakeSize
Returns: NSData
5.1.20 NSDataToArray
Returns: Byte()
5 B4i NativeObject 36 B4X JavaObject NativeObject
5.1.21 RunMethod
Runs the given method. Params can be Null if there are no parameters.
Note that a method name is the full name with the colons.
Returns: B4INativeObject
5.1.22 RunMethodWithBlocks
Returns: B4INativeObject
5.1.23 SetField
Returns: Void
5.1.24 Tag
Gets or sets the Tag object. This is a placeholder for any object you like to tie to this object.
Readable As NSObject
Writeable As NSObject
5.1.25 UIColorToColor
Returns: Int
5.2 B4i NativeObject How to use it 37 B4X JavaObject NativeObject
5.3.1 SetButtonTextColor
SetButtonTextColor(Button1, Colors.Grean, 1)
#PlistExtra: <key>UIViewControllerBasedStatusBarAppearance</key><false/>
#PlistExtra: <key>UIViewControllerBasedStatusBarAppearance</key><false/>
Sub SetStatusBarStyleLight
Dim no As NativeObject = app
no.RunMethod("setStatusBarStyle:", Array(1))
End Sub