Search code examples
androidkotlinhighcharts

How to detect when user lifted a finger from the chart in Android


I have an XML layout element like this

    <com.highsoft.highcharts.core.HIChartView
        android:id="@+id/chart"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:contentDescription="@{data.chartTypeContentDescription}"
        bind:layout_height="@{data.chartHeight}"
        bind:layout_width="@{data.chartWidth}"
        tools:layout_height="150dp" />

What I want to achieve is if the user lifts his/hers finger from the line chart (HIArea()), then I would like to do something with this. As far as I know there is not really a native support for this from the Highcharts library.

The closest I got is to use setOnTouchListener but this does not seem to detect any touch event coming from the chart.

    binder.chart.rootView.setOnTouchListener { v, event ->
        Log.d("eqweqeqew", "rootView onTouch: ${event.action}")
        when (event.action) {
            MotionEvent.ACTION_UP -> {
                Log.d("eqweqeqew", "rootView onTouch: MotionEvent.ACTION_DOWN")
            }
        }
        true
    }

    binder.chart.setOnTouchListener { v, event ->
        Log.d("eqweqeqew", "chart onTouch: ${event.action}")
        when (event.action) {
            MotionEvent.ACTION_UP -> {
                Log.d("eqweqeqew", "chart onTouch: MotionEvent.ACTION_DOWN")
            }
        }
        true
    }

Both of them are not really working.


Solution

  • In Highcharts Android integration a WebView is used underneath, so all user interaction events are closely tied to JavaScript events. I'd suggest using wrapper events and functions. Here's a helpful example of how to use functions in the Highcharts Android integration: HiFunction Example.

    However, some events, such as mouseOut, may not work in the same way because they simply do not make sense on touchscreen devices. The best solution would be to use events that overlap with those on touchscreen devices (e.g., click),to ensure that they will work as expected.