Thursday, December 17, 2020

Android Kotlin exercise: detect touch on individual view

 This example show how to detect touch on individual view.


Example code

package com.blogspot.android_er.exmyview

import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MotionEvent
import android.widget.ImageView
import android.widget.TextView

/*
ref: 
https://developer.android.com/training/gestures/detector#capture-touch-events-for-a-single-view
Android Developers Guides > Detect common gestures > Capture touch events for a single view
 */

class MainActivity : AppCompatActivity() {
    private lateinit var ivIcon: ImageView
    private lateinit var tvSysInfo: TextView
    private lateinit var tvSdkInfo: TextView
    private lateinit var tvConsole: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        ivIcon = findViewById(R.id.icon)
        tvSysInfo = findViewById(R.id.sysinfo)
        tvSdkInfo = findViewById(R.id.sdkinfo)
        tvConsole = findViewById(R.id.console)

        val manufacturer = Build.MANUFACTURER
        val model = Build.MODEL
        val release = Build.VERSION.RELEASE

        val pkgInfo = packageManager.getPackageInfo(packageName, 0)
        val appInfo = pkgInfo.applicationInfo
        val targetSdkVersion = appInfo.targetSdkVersion
        val minSdkVersion = appInfo.minSdkVersion

        tvSysInfo.text = manufacturer + "\n" +
                model + "\n" + "Android: " + release
        tvSdkInfo.text = "targetSdkVersion: " + targetSdkVersion + "\n" +
                "minSdkVersion: " + minSdkVersion

        tvConsole.text = "Android Example:" +
                "\nCapture touch events for a single view"

        ivIcon.setOnTouchListener { v, event -> // ... Respond to touch events
            /*
            return true if the listener has consumed the event, false otherwise.
             */
            val action = event.action
            when (action) {
                MotionEvent.ACTION_DOWN -> {
                    /*
                    Beware of creating a listener that returns false
                    for the ACTION_DOWN event.
                    If you do this, the listener will not be called
                    for the subsequent ACTION_MOVE and ACTION_UP string of events.
                    This is because ACTION_DOWN is the starting point for all touch events.
                     */

                    tvConsole.text = "ACTION_DOWN@ivIcon"

                    true}
                MotionEvent.ACTION_MOVE -> {
                    tvConsole.append("\nACTION_MOVE@ivIcon")
                    true}
                MotionEvent.ACTION_UP -> {
                    tvConsole.text = "ACTION_UP@ivIcon"
                    true}
                MotionEvent.ACTION_CANCEL -> {
                    tvConsole.text = "ACTION_CANCEL@ivIcon"
                    true}
                MotionEvent.ACTION_OUTSIDE -> {
                    tvConsole.text = "ACTION_OUTSIDE@ivIcon"
                    true}
                else -> super.onTouchEvent(event)
            }

        }

        tvSysInfo.setOnTouchListener { v, event -> // ... Respond to touch events

            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    tvConsole.text = "ACTION_DOWN@tvSysInfo"
                    true}
                MotionEvent.ACTION_MOVE -> {
                    tvConsole.append("\nACTION_MOVE@tvSysInfo")
                    true}
                MotionEvent.ACTION_UP -> {
                    tvConsole.text = "ACTION_UP@tvSysInfo"
                    true}
                MotionEvent.ACTION_CANCEL -> {
                    tvConsole.text = "ACTION_CANCEL@tvSysInfo"
                    true}
                MotionEvent.ACTION_OUTSIDE -> {
                    tvConsole.text = "ACTION_OUTSIDE@tvSysInfo"
                    true}
                else -> super.onTouchEvent(event)
            }
        }

        tvSdkInfo.setOnTouchListener { v, event -> // ... Respond to touch events

            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    tvConsole.text = "ACTION_DOWN@tvSdkInfo"
                    true}
                MotionEvent.ACTION_MOVE -> {
                    tvConsole.append("\nACTION_MOVE@tvSdkInfo")
                    true}
                MotionEvent.ACTION_UP -> {
                    tvConsole.text = "ACTION_UP@tvSdkInfo"
                    true}
                MotionEvent.ACTION_CANCEL -> {
                    tvConsole.text = "ACTION_CANCEL@tvSdkInfo"
                    true}
                MotionEvent.ACTION_OUTSIDE -> {
                    tvConsole.text = "ACTION_OUTSIDE@tvSdkInfo"
                    true}
                else -> super.onTouchEvent(event)
            }
        }
    }
}

layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/leftpanel"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        android:background="#E0E0E0"
        android:layout_margin="5dp">

        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:padding="5dp"
            android:background="#0000F0"
            android:src="@mipmap/ic_launcher_round"/>
        <TextView
            android:id="@+id/sysinfo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:padding="5dp"
            android:background="#0000D0"
            android:textSize="20dp"
            android:textStyle="bold"
            android:textColor="@color/white"/>
        <TextView
            android:id="@+id/sdkinfo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:padding="5dp"
            android:background="#0000B0"
            android:textSize="20dp"
            android:textStyle="bold"
            android:textColor="@color/white"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/rightpanel"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        android:background="#C0C0C0">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:padding="5dp"
            android:textSize="20dp"
            android:textStyle="bold"
            android:textColor="@color/white"
            android:text="android-er.blogspot.com"/>
        <TextView
            android:id="@+id/console"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:padding="5dp"
            android:textSize="30dp"
            android:textStyle="bold"
            android:gravity="bottom"/>
    </LinearLayout>
</LinearLayout>

No comments: