Search code examples
javaandroidtextviewgesture

"Swipe Detection" attached to textview is malfunctioning


I am trying to detect "Left Swipe", "Right Swipe" and "Down Swipe".

Please have a look at the Following code

ParagraphReader.java

package k.k;

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

import android.os.Bundle;
import android.app.Activity;
import android.speech.tts.TextToSpeech;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ParagraphReader extends Activity implements TextToSpeech.OnInitListener {

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

    private TextToSpeech tts;

    @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);

        paraListQuestionIndicator = (TextView)findViewById(R.id.paralist_question_indicator);

        speaker = (ImageView)findViewById(R.id.speak);
        speaker.setOnClickListener(new SpeakOutTheText());

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

        tts = new TextToSpeech(this,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));
        setParaListQuestionIndicator(currentQuestion);


    }

    @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();
            currentQuestion--;

            if(currentQuestion<0)
            {
                currentQuestion = paraList.size()-1;
            }

            paraText.setText(paraList.get(currentQuestion));    
            setParaListQuestionIndicator(currentQuestion);
            return true;
         }

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

              if(currentQuestion>paraList.size()-1)
              {
                  currentQuestion = 0;
              }

              paraText.setText(paraList.get(currentQuestion));
              setParaListQuestionIndicator(currentQuestion);

                return true;
          }

          public boolean onSwipeBottom()
          {
              return true;
          }


    };

    private class SpeakOutTheText implements OnClickListener
    {

        @Override
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            speak(paraText.getText().toString());

        }

    }

    private void setParaListQuestionIndicator(int number)
    {
        paraListQuestionIndicator.setText((number+1)+"/"+paraList.size());
    }



    @Override
    public void onInit(int status) 
    {
        // TODO Auto-generated method stub
        if(status==TextToSpeech.SUCCESS)
        {
            int result = tts.setLanguage(Locale.UK);

            if(result==TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)
            {
                Toast.makeText(this, "Language Not Supported", Toast.LENGTH_LONG).show();
            }
        }


    }

    @Override
    public void onDestroy()
    {
        if(tts!=null)
        {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }



    public void speak(String text)
    {
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }

}

ParagraphReader.xml

<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:minHeight="200dp"
            android:ems="30"
            android:singleLine="false"
            android:clickable="true"
            >


        </TextView>

    </ScrollView>

    <TextView
        android:id="@+id/paralist_question_indicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/speak"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"
        android:src="@drawable/speaker2" />

</RelativeLayout>

And the Gesture detection code,

OnSwipeTouchListener.java

This code is from here

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;
    }
}

This code works fine as it should in Virtual Device, but to my surprise, it is not working on real device! I have Micromax Canvas2, and I tested with my phone and HP phone, in both this is not working.

Not working means, I think it do not identify any of the swipes properly. "Sometimes" it does swipe to left and right, but that is "sometimes" and for that also I had to drag it very hard 3-4 times.

What is wrong here? This is not working because I implemented the listener to a TextView?

PS: I posted the completed related classes here because last time it was impossible to get a suitable answer with code removed.


Solution

  • I found the answer. The case is, here what I have is a ScrollView which is by default responding to swipes and my text view was inside that. So, the only fix was taking the TextView out of the ScrollView.