Gesture detecter not working

562 views Asked by At

I have a set of java code which i try to detect touch gestures by users. When a user do a simple touch/swipe down etc, a text view will display out what the user has currently done. However when i run the code on my emulator, it is just a black screen which show Hello World! and when i do a touch, nothing is display.. why is that so? Attached is the code. Thanks for your help...

package org.tp.iit.cds.BrailleTypeSend;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector;
import android.widget.TextView;
import android.graphics.Color;


public class BrailleSend extends Activity implements OnGestureListener {
    /** Called when the activity is first created. */
    public LinearLayout main;    
    public TextView viewA;

    public GestureDetector gestureScanner;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gestureScanner = new GestureDetector(this);

        main = new LinearLayout(this);
        main.setBackgroundColor(Color.GRAY);
        main.setLayoutParams(new LinearLayout.LayoutParams(320,480));

        viewA = new TextView(this);
        viewA.setBackgroundColor(Color.YELLOW);
        viewA.setTextColor(Color.WHITE);
        viewA.setTextSize(16);
        viewA.setLayoutParams(new LinearLayout.LayoutParams(320,80));
        main.addView(viewA);

        setContentView(R.layout.main);
    }

    @Override
    public boolean onDown(MotionEvent e) {  
        viewA.setText("Down Stroke");
        return true;
    }

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

} 
2

There are 2 answers

1
Samuh On

Three things:

  • All you are doing with this code is creating bunch of objects on the heap and setting some properties. These view objects that you've created are not even appended to the View hierarchy.
gestureScanner = new GestureDetector(this);
main = new LinearLayout(this);
main.setBackgroundColor(Color.GRAY);
main.setLayoutParams(new LinearLayout.LayoutParams(320,480));
viewA = new TextView(this);
viewA.setBackgroundColor(Color.YELLOW);
viewA.setTextColor(Color.WHITE);
viewA.setTextSize(16);
viewA.setLayoutParams(new LinearLayout.LayoutParams(320,80));
main.addView(viewA);
  • The View heirarchy of your activity comes from the main.xml file in your layout folder since you've written: setContentView(R.layout.main); in your code.

  • You have not attached GestureListener to anything. How do you expect Callbacks to be invoked?

0
Navigatron On

I am not sure if you need a gesture overlay for GestureDetector, but I do know that you need to add a gesture overlay to your view if you use OnGesturePerformedListener. A gesture overlay acts as a drawing board for the user.

GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);

And the XML:

<android.gesture.GestureOverlayView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gestures"
android:layout_width="fill_parent"
android:layout_height="fill_parent"

android:gestureStrokeType="multiple"
android:eventsInterceptionEnabled="true"
android:orientation="vertical">

<ListView
    android:id="@android:id/list"  
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"  />

More information about it here