Android Tutorial [Advanced] Using a custom view written with Kotlin

Discussion in 'Tutorials & Examples' started by Erel, Mar 25, 2019.

  1. Erel

    Erel Administrator Staff Member Licensed User

    [​IMG]

    Using Kotlin libraries is similar to using Java libraries. The only difference is that we need to add references to two additional libraries:
    http://central.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib/1.3.21/kotlin-stdlib-1.3.21.jar
    http://central.maven.org/maven2/org...lib-jdk7/1.3.21/kotlin-stdlib-jdk7-1.3.21.jar
    Code:
    #AdditionalJar: kotlin-stdlib-1.3.21
    #AdditionalJar: kotlin-stdlib-jdk7-1.3.21
    As an example, I wanted to show how to use this open source project, without writing a wrapper: https://github.com/iammert/CameraVideoButton

    The first step is to get the jar or AAR library from the maven repository. I like the direct approach. (The indirect approach is to create an Android Studio project based on the instructions and then find the downloaded resource.)

    1. Get the compiled library

    The setup instructions from that page:
    Code:
    allprojects {
       repositories {
           ...
           maven { url 
    'https://jitpack.io' }
       }
    }

    dependencies {
       implementation 'com.github.iammert:CameraVideoButton:0.2'
    }
    This can be translated to:

    https://jitpack.io/com/github/iammert/CameraVideoButton/0.2/
    Follow this link to see the list of files available.

    We can then download the aar file: https://jitpack.io/com/github/iammert/CameraVideoButton/0.2/CameraVideoButton-0.2.aar
    The other interesting file is the POM file. It lists the other dependencies. Note that the other dependencies might depend on more resources.

    2. Calling the constructor

    We need to look in the code and find the constructor: https://github.com/iammert/CameraVi...ary/cameravideobuttonlib/CameraVideoButton.kt
    The B4A code:
    Code:
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    CameraVideoButton.InitializeNewInstance(
    "com.iammert.library.cameravideobuttonlib.CameraVideoButton", _
         
    Array(ctxt, Null0))
    Once we see that it is working we translate the example code and also create the listener:
    Code:
    Dim duration As Long = 10000
    CameraVideoButton.RunMethod(
    "setVideoDuration"Array(duration))
    CameraVideoButton.RunMethod(
    "enableVideoRecording"Array(True))
    CameraVideoButton.RunMethod(
    "enablePhotoTaking"Array(True))
    Dim listener As Object = CameraVideoButton.CreateEventFromUI("com.iammert.library.cameravideobuttonlib.CameraVideoButton.ActionListener", _
       
    "VideoListener"Null)
    CameraVideoButton.RunMethod(
    "setActionListener"Array(listener))
    Activity.AddView(CameraVideoButton, 50%x - 50dip100%y - 120dip100dip100dip)

    Sub VideoListener_Event (MethodName As String, Args() As Object) As Object
       
    Log(MethodName)
       
    Return Null
    End Sub
    Note that the duration parameter type is Long. I first wrote Array(10000) and it failed with a class cast exception.

    At this point everything worked but I wasn't able to change the "recording color" value. The library doesn't provide a method to set it programmatically. It is extracted from the view's XML declaration:
    Code:
    recordingColor = typedArray.getColor(R.styleable.CameraVideoButton_cvb_recording_color, Color.WHITE)
    We can set it by creating a custom theme with the manifest editor:
    Code:
    SetApplicationAttribute(android:theme, "@style/Theme")

    CreateResource(values, theme.xml,
    <resources>
        <style name=
    "Theme" parent="@android:style/Theme.Material">
            <item name=
    "cvb_recording_color">#ffff0000</item>
        </style>
    </resources>
    )
    Note that the material theme is only available on Android 5+. I've set minSdkVersion to 21 (Android 5). It shouldn't be difficult to use an AppCompat theme instead or to to create a different theme based on the Android version if you want to support Android 4 devices: https://www.b4x.com/android/forum/threads/87716/#content


    The project is attached.
     

    Attached Files:

    Last edited: Mar 25, 2019
    Jamie8763, Mashiane, ocalle and 16 others like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice