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>

Friday, December 11, 2020

Linux Mint: Add Program Launcher to Menu

As a example, this video show how to Add Program Launcher to Menu on Linux Mint 20, to run idea.sh in /opt/idea-IC-203.5981.155/bin folder.

Thursday, December 10, 2020

Install IntelliJ IDEA on Linux Mint 20

Steps to install Jetbrains IntelliJ IDEA Community Edition 2020.3 on Linux Mint 20, tested on VirtualBox.
 

IntelliJ IDEA is a cross-platform IDE on the Windows, macOS, and Linux operating systems. It is available in the following editions:

  • Community Edition is free and open-source, licensed under Apache 2.0. It provides all the basic features for JVM and Android development.
  • IntelliJ IDEA Ultimate is commercial, distributed with a 30-day trial period. It provides additional tools and features for web and enterprise development.

To install IntelliJ IDEA Community Edition on Linux Mint:

Visit: https://www.jetbrains.com/idea/download/

Download .tar.gz of Community Edition.

Extract the tarball to a directory that supports file execution.
For example, to extract it to the recommended /opt directory, run the following command:

$ sudo tar -xzf <downloaded file> -C /opt

Execute the idea.sh script from the extracted directory to run IntelliJ IDEA. or add a program launcher to Linux Mint Menu to run idea.sh.

ref: https://www.jetbrains.com/help/idea/installation-guide.html