Question

Okay, heres the problem. I have made a widget, and now need to detect tap and double tap on it, which when triggered should show a different view. For testing purposes I used logging, to see if it is working. Below is my code:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.Toast;

/**
 * @Author Jan Zivkovic
 * 
 * NE DELA, OVERRIDA AMPAK NE DELA!
 */

public class UpdateWidget extends Activity implements OnGestureListener{

    private GestureDetector gesture;

    public void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  
        //setContentView(R.layout.main);  
        Log.w("TEST", "started");


        gesture = new GestureDetector(this);  

        //set the on Double tap listener  
        gesture.setOnDoubleTapListener(new OnDoubleTapListener()  
        {  
            @Override  
            public boolean onDoubleTap(MotionEvent e)  
            {  
                //set text color to green  
                //tvTap.setTextColor(0xff00ff00);  
                //print a confirmation message  
                //tvTap.setText("The screen has been double tapped.");  
                Log.w("TEST", "Double tap");
                return false;  
            }  

            @Override  
            public boolean onDoubleTapEvent(MotionEvent e)  
            {  
                //if the second tap hadn't been released and it's being moved  
                if(e.getAction() == MotionEvent.ACTION_MOVE)  
                {  
                    //set text to blue  
                    //tvTapEvent.setTextColor(0xff0000ff);  
                    //print a confirmation message and the position  
                    //tvTapEvent.setText("Double tap with movement. Position:\n"  
                    //        + "X:" + Float.toString(e.getRawX()) +  
                    //        "\nY: " + Float.toString(e.getRawY()));  
                }  
                else if(e.getAction() == MotionEvent.ACTION_UP)//user released the screen  
                {  
                    //setContentView();  
                }  
                return false;  
            }  

            @Override  
            public boolean onSingleTapConfirmed(MotionEvent e)  
            {  
                //set text color to red  
                //tvTap.setTextColor(0xffff0000);  
                //print a confirmation message and the tap position  
                //tvTap.setText("Double tap failed. Please try again.");  
                Log.w("TEST", "Single tap");
                return false;  
            }  
        });  
    }

    @Override
    public boolean onDown(MotionEvent arg0) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
            float arg3) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onLongPress(MotionEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
            float arg3) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onShowPress(MotionEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent arg0) {
        // TODO Auto-generated method stub
        return false;
    }

}

Widget Provider class, onUpdate method (triggered every 5 minutes)

@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        for (int i = 0; i < appWidgetIds.length; i++) {
            int appWidgetId = appWidgetIds[i];
            Intent updateIntent = new Intent(context, UpdateWidget.class);
            // Identifies the particular widget...
            updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            updateIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // Make the pending intent unique...
            updateIntent.setData(Uri.parse(updateIntent.toUri(Intent.URI_INTENT_SCHEME)));
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, updateIntent, 0);


            //context.startActivity(updateIntent);

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);//R.layout.widget_layout
            views.setTextViewText(R.id.subject1, "subject1 test");
            views.setOnClickPendingIntent(R.id.widget_layout, pendingIntent);
            appWidgetManager.updateAppWidget(appWidgetId, views);
            Log.w("Schedule", "Widget Updated = " + appWidgetId);
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

But heres what it gotten to be so far. Normaly when you tap or double tap on a widget, the phone vibrates. Using this, the phone doesen't vibrate anymore when tapped nor it prints anything in logcat. This problem is annoying me for the last couple of days. Hopefully someone else will know what I am doing wrong.

EDIT: Forgot the most important thing. Im using Android 2.2 api level 8 (Froyo) And testing on Samsung Galaxy Ace VE with Android 2.3.6

Was it helpful?

Solution

I have made a widget

Given your code, I am going to assume that you mean what other Android developers refer to as an "app widget".

and now need to detect tap and double tap on it

Double-taps will be difficult to detect.

Below is my code

Your first block of code is for an activity, which is not an app widget.

Your second block of code invokes an activity when the user taps on an app widget. That is fine, but that will not help you detect double taps. In fact, it will generally prevent the user from doing a double-tap in the first place, as the activity will take over the screen after the first tap.

I would counsel you to avoid trying to use a double tap with an app widget. Have two distinct items in the app widget to distinguish the different events (whatever you wanted a single tap to do versus whatever you wanted a double tap to do).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top