• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps GestureDetector 2 Views

Fred Pino

Lurker
Hello guys,
Please I would like some help on this.
I have 2 Buttons that would receive the same touch types and perform different actions which is a message that should appear in TextView depending on touch type.
Please guide me through this
Thank you.

here is my code :

Java:
package com.example.vuelve;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnTouchListener,
        GestureDetector.OnDoubleTapListener,
        GestureDetector.OnGestureListener {

    public Button GreenBoton, RedBoton;
    public TextView Message;
    public GestureDetector GD;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GreenBoton = findViewById(R.id.IDBotonV);
        RedBoton = findViewById(R.id.IDBotonR);
        Message = findViewById(R.id.IDTexto);

        GD = new GestureDetector(this, this);
        GD.setOnDoubleTapListener(this);
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        this.GD.onTouchEvent(event);
        event.getAction();

        if(v.getId()== R.id.IDBotonV){

            if(onSingleTapConfirmed(event)){Message.setText("Blue SingleTap");}
            if(onDoubleTap(event)){Message.setText("Blue onDoubleTap");}
            if(onDoubleTapEvent(event)){Message.setText("Blue DoubleTap");}
            if(onDown(event)){Message.setText("Blue Down");}}

        if(v.getId()== R.id.IDBotonR){

            if(onSingleTapConfirmed(event)){Message.setText("Red SingleTap");}
            if(onDoubleTap(event)){Message.setText("Red onDoubleTap");}
            if(onDoubleTapEvent(event)){Message.setText("Red DoubleTap");}
            if(onDown(event)){Message.setText("Red Down");}
        }
        return true;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        GreenBoton.setOnTouchListener(this);
        RedBoton.setOnTouchListener(this);

        return false;
    }
    @Override
    public boolean onDoubleTap(MotionEvent e) {
        GreenBoton.setOnTouchListener(this);
        RedBoton.setOnTouchListener(this);
        return true;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        GreenBoton.setOnTouchListener(this);
        RedBoton.setOnTouchListener(this);
        return true;
    }
    @Override
    public boolean onDown(MotionEvent e) {
        GreenBoton.setOnTouchListener(this);
        RedBoton.setOnTouchListener(this);
        return true;
    }
    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }
    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return false;
    }
}

Activity Main XML

Code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="#64AEE9"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/IDBotonV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="50dp"
        android:layout_marginTop="240dp"
        android:background="#4CAF50"
        android:text="Verde"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/IDTexto" />

    <TextView
        android:id="@+id/IDTexto"
        android:layout_width="242dp"
        android:layout_height="56dp"
        android:layout_marginTop="124dp"
        android:background="#FFEB3B"
        android:gravity="center_horizontal|center_vertical"
        android:hint="Motion"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/IDBotonR"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="240dp"
        android:layout_marginEnd="50dp"
        android:background="#F44336"
        android:text="Rojo"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/IDTexto" />
</androidx.constraintlayout.widget.ConstraintLayout>
 
You mean when you click on the button, something should be shown on textView based on touch types ?
 
Back
Top Bottom