Search code examples
javaandroidxmlgesturedetector

GestureDetector not working on new layout


I have created the standard gesture detector that changes a textview depending on the given gesture. All works fine when my XML contains only the relative layout and the text view I wish to change. However when I add a gird layout and a few icons the Gesture Detector no longer works, simply not changing the textview as it did before. The code is;

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/Relative_Layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/homebackground"
tools:context="com.example.james.fitness.MainActivity"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">

    <GridLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:id="@+id/Grid1">

        <TextView
            android:id="@+id/someText"
            android:text="sometext"
            android:foregroundGravity="center_vertical|center_horizontal"
            android:gravity="center_vertical|center"
            android:layout_gravity="center_vertical|center_horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleX="5"
            android:scaleY="5"
            android:textColor="@color/colorAccent" />

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            app:srcCompat="@drawable/fitnessbutton"
            android:id="@+id/fitnessbutton"
            android:layout_row="1"
            android:layout_column="1"
            android:layout_columnWeight="1"
            android:layout_gravity="fill_horizontal"
            android:gravity="center"
            android:paddingBottom="100dp" />

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            app:srcCompat="@drawable/drinkbutton"
            android:id="@+id/drinkbutton"
            android:layout_column="2"
            android:layout_row="1"
            android:layout_columnWeight="1"
            android:layout_gravity="fill_horizontal"
            android:gravity="center"
            android:paddingBottom="100dp" />

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            app:srcCompat="@drawable/foodbutton"
            android:id="@+id/foodbutton"
            android:layout_column="3"
            android:layout_row="1"
            android:layout_columnWeight="1"
            android:layout_gravity="fill_horizontal"
            android:gravity="center"
            android:paddingBottom="100dp" />
    </GridLayout>
</RelativeLayout>

And the Java

import android.app.Activity;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;



public class MainActivity extends Activity implements
        GestureDetector.OnGestureListener,
        GestureDetector.OnDoubleTapListener {


    private TextView someText;
    private GestureDetectorCompat gestureDetector;


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

        someText = (TextView)findViewById(R.id.someText);
        this.gestureDetector = new GestureDetectorCompat(this, this);
        gestureDetector.setOnDoubleTapListener(this);
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        someText.setText("SINGLETAP");
        return true;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        someText.setText("DOUBLETAP");
        return true;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        someText.setText("DOUBLETAPEVENT");
        return true;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        someText.setText("DOWN");
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        someText.setText("PRESS");

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        someText.setText("SINGLETAPUP");
        return true;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        someText.setText("SCROLL");
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        someText.setText("LONGPRESS");
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        someText.setText("FLING");
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        gestureDetector.onTouchEvent(event);
        return false;

    }

}

Note if I comment out the grid layout and the image views then the code works again changing the textview. I can only presume the gesture detector is now buried beneath the grid layout but aren't sure?


Solution

  • Try to remove this lines in your layout, they are useless in your case

    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true"