Search code examples
javaandroideclipsegestureontouchlistener

OnTouchListener is not getting fired


Please have a look at the following code

XML Code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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"
    tools:context=".ParagraphReader" >

    <ScrollView
        android:id="@+id/paragraph_reader_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:id="@+id/paragraph_reader_txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="30"
            android:singleLine="false"
            android:enabled="true"
            >


        </TextView>

    </ScrollView>






</RelativeLayout>

Java Code

package k.k;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ParagraphReader extends Activity {

    private TextView paraText;
    private DatabaseConnector database;
    private List<String>paraList;
    private int currentQuestion;

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

        paraText = (TextView)findViewById(R.id.paragraph_reader_txt);
        paraText.setOnTouchListener(paraSwiped);

        paraList = new ArrayList<String>();
        database = DatabaseHandler.getInstance(this);

        //Get the Paragraph list
        int listNumber = getIntent().getIntExtra("PARAGRAPH_LIST", 0);

        Toast.makeText(this, "Selected Paragraph: "+listNumber, Toast.LENGTH_LONG).show();

        paraList = database.getParagraphList(listNumber);
        Toast.makeText(this, "ParaList size "+paraList.size(), Toast.LENGTH_LONG).show();

        //Toast.makeText(this, "Size: "+paraList.size(), Toast.LENGTH_LONG).show();
        paraText.setText(paraList.get(0));

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.paragraph_reader, menu);
        return true;
    }


    //The Event Handler for the Paragraph Text holder
    OnTouchListener paraSwiped = new OnSwipeTouchListener()
    {
        public boolean onSwipeRight() 
         {
            Toast.makeText(ParagraphReader.this, "Right: "+paraList.size(), Toast.LENGTH_SHORT).show();


            return true;
         }

          public boolean onSwipeLeft() 
          {
              Toast.makeText(ParagraphReader.this, "Left: "+paraList.size(), Toast.LENGTH_SHORT).show();

                return true;
          }


    };

}

Here, you can see I have implemented an OnTouchListener to the TextView. Below is the code for the OnTouchListener class. This code is built by one of SO Member.

package k.k;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class OnSwipeTouchListener implements OnTouchListener {

    private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());

    public boolean onTouch(final View v, final MotionEvent event) {
        return gestureDetector.onTouchEvent(event);

    }

    private final class GestureListener extends SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

        @Override
        public boolean onDown(MotionEvent e) {
            return super.onDown(e);
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            result = onSwipeRight();
                        } else {
                            result = onSwipeLeft();
                        }
                    }
                } else {
                    if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffY > 0) {
                            result = onSwipeBottom();
                        } else {
                            result = onSwipeTop();
                        }
                    }
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }

    public boolean onSwipeRight() {
        return false;
    }

    public boolean onSwipeLeft() {
        return false;
    }

    public boolean onSwipeTop() {
        return false;
    }

    public boolean onSwipeBottom() {
        return false;
    }
}

I need only left and right swipes, but due to some reason, the swiping is not working. Yes, it is not working, which means nothing is happening. I do not understand how to correct this. I know the code developed by the SO member is working because I am using it in another activity.


Solution

  • You might try making the text view clickable with android:clickable="true". Not sure that will fix the problem but it's worth a shot. I'm not sure what android:enabled does so it might be doing the same thing