Android Studio: Replace OnClickListener to OnTouchListener

14 views Asked by At

I have three clickable views contained in an Array. I am currently trying to implement an OnTouchListener where if I hover on the boxes, it will say that it has been clicked.

For example, when I press and drag the screen then hover my finger on view1 and view 2, it will say "view 1 clicked" & "view 2 clicked".

XML Code

<RelativeLayout 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"
    tools:context=".MainActivity"
    android:background="@color/white">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <View
            android:id="@+id/view1"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_weight="0.5"
            android:layout_gravity="center"
            android:layout_marginLeft="5dp"
            android:background="@color/teal_200"/>
        <View
            android:id="@+id/view2"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_marginLeft="5dp"
            android:layout_weight="0.5"
            android:layout_gravity="center"
            android:background="@color/teal_200" />

        <View
            android:id="@+id/view3"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="0.5"
            android:layout_gravity="center"
            android:background="@color/teal_200" />
    </LinearLayout>

</RelativeLayout>

Java Code

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    private static final int[] idArray = {R.id.view1, R.id.view2, R.id.view3};
    private View[] view = new View[idArray.length];
    int i;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        for (i = 0; i < idArray.length; i++){
            view[i] = (View)findViewById(idArray[i]);
            view[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (v.getId()) {
                        case R.id.view1:
                            Toast.makeText(MainActivity.this, "View 1 Clicked", Toast.LENGTH_SHORT).show();
                            break;
                        case R.id.view2:
                            Toast.makeText(MainActivity.this, "View 2 Clicked", Toast.LENGTH_SHORT).show();
                            break;
                        case R.id.view3:
                            Toast.makeText(MainActivity.this, "View 3 Clicked", Toast.LENGTH_SHORT).show();
                            break;
                    }
                }
            });
        }
    }
}

I have tried placing Motion Event ACTION_DOWN then ACTION_MOVE to drag and press, hence ACTION_UP to cancel. Yet, I'm having issues how to put an array along with touchlistener/motionevent.

0

There are 0 answers