Android native widgets

Ali Lloyd ali.lloyd at livecode.com
Wed Jun 21 17:59:33 EDT 2017


Further to Kevin's note about creating native widgets on Android in another
thread, I thought I'd post the LCB template here that gets you all the
android native widgets (that is, classes in the android.widget package -
see here:
https://developer.android.com/reference/android/widget/package-summary.html)

I have set it up so you can simply use the merge function with the
appropriate value of tWidget - Button, DatePicker, EditText (i.e. native
field), etc

widget com.livecode.widget.native.android.[[tWidget]]

use com.livecode.foreign
use com.livecode.java
use com.livecode.widget
use com.livecode.canvas
use com.livecode.engine
use com.livecode.library.widgetutils

metadata version is "1.0.0"
metadata author is "LiveCode"
metadata title is "Android Native [[tWidget]]"


__safe foreign handler _JNI_GetAndroidEngine() returns JObject \
   binds to
"java:com.runrev.android.Engine>getEngine()Lcom/runrev/android/Engine;!static"
__safe foreign handler _JNI_GetEngineContext(in pEngine as JObject) returns
JObject \
   binds to "java:android.view.View>getContext()Landroid/content/Context;"

// Handlers for creating and attaching view
__safe foreign handler _JNI_CreateView(in pContext as JObject) returns
JObject \
   binds to
"javaui:android.widget.[[tWidget]]>new(Landroid/content/Context;)"
__safe foreign handler _JNI_AddView(in pParentView as JObject, in
pChildView as JObject) returns nothing \
   binds to "javaui:android.view.ViewGroup>addView(Landroid/view/View;)V"

private variable mNativeObj as optional JObject
private variable mOpen as Boolean

private handler IsAndroid() returns Boolean
    return the operating system is "android"
end handler

public handler OnCreate()
    put false into mOpen
end handler

private handler InitView()
// Create an android button using the Engine Context
    variable tEngine as JObject
    put _JNI_GetAndroidEngine() into tEngine

    variable tContext as JObject
    put _JNI_GetEngineContext(tEngine) into tContext
    put _JNI_CreateView(tContext) into mNativeObj

// put my native window into tParent
    variable tParent as Pointer
    MCWidgetGetMyStackNativeView(tParent)

    // wrap the parent pointer
    variable tParentObj as JObject
    put PointerToJObject(tParent) into tParentObj

    // add the view
    _JNI_AddView(tParentObj, mNativeObj)

    // get the pointer from the view and set the native layer
    variable tPointer as Pointer
    put PointerFromJObject(mNativeObj) into tPointer
    set my native layer to tPointer
end handler

private handler FinalizeView()
    set my native layer to nothing
    put nothing into mNativeObj
end handler

public handler OnOpen()
    put true into mOpen
    if IsAndroid() then
       InitView()
    end if
end handler

public handler OnClose()
   if IsAndroid() then
      FinalizeView()
   end if
    put false into mOpen
end handler

public handler OnPaint()
    if IsAndroid() then
        return
    end if

    fill text "Native [[tWidget]]" at center of my bounds on this canvas
end handler

end widget

I've made a screenshot with a selection of widgets here:
https://www.dropbox.com/s/67tm95mnniwobqt/Screenshot_2017-06-21-22-46-04.png

Obviously none of these things actually do anything until you hook into
their properties and listeners, but it is relatively straightforward to do
this. If you're interested you'll need to look up the individual class in
the android API and find the appropriate methods - the button example
should give you an idea of how to do this:
https://github.com/livecode/livecode/blob/develop/extensions/widgets/androidbutton/androidbutton.lcb



More information about the use-livecode mailing list