0

I am trying to add view flipper in app widget. It has to simply flips between 2 images with swipe gestures. But nothing is getting displayed and its crashing. I have the code for the activity so I translated the similar for widget with appropriate changes. But somewhere, I am missing something. Please guide. Here is the code.

public class WidgetFlipper extends AppWidgetProvider{

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    private ViewFlipper vf;
    private Context mContext;

    private final GestureDetector detector = new GestureDetector(new MyGestureDetector());

    public MyGestureDetector obj = new MyGestureDetector(); 

    @SuppressLint("NewApi")
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int []appWidgetIds){

        super.onUpdate(context, appWidgetManager, appWidgetIds);
        mContext = context;
        RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.main);
        vf.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(final View view, final MotionEvent event) {
                detector.onTouchEvent(event);
                return true;
            }
        });


        views.setImageViewResource(R.id.vfShow, R.drawable.image1);
        views.setImageViewResource(R.id.vfShow, R.drawable.image2);
        Intent intent = new Intent ("android.appwidget.action.APPWIDGET_UPDATE");

        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.vfShow, pendingIntent);

        ComponentName myWidget=new ComponentName(context, WidgetFlipper.class);
        AppWidgetManager manager=AppWidgetManager.getInstance(context);
        manager.updateAppWidget(myWidget, views);

    }


    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            try {

                // right to left swipe
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    vf.setInAnimation(AnimationUtils.loadAnimation(mContext,
                            R.anim.left_in));
                    vf.setOutAnimation(AnimationUtils.loadAnimation(mContext,
                            R.anim.left_out));
                    vf.showNext();
                    return true;
                } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    vf.setInAnimation(AnimationUtils.loadAnimation(mContext,
                            R.anim.right_in));
                    vf.setOutAnimation(AnimationUtils.loadAnimation(mContext,
                            R.anim.right_out));
                    vf.showPrevious();
                    return true;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

            return false;
        }
    }

}
Atihska
  • 4,803
  • 10
  • 56
  • 98

1 Answers1

0

Found out that its not possible what I was trying to do. Its not a a feasible design. Swipe within a swipe!!..That's a design flaw if it would have been possible..

Atihska
  • 4,803
  • 10
  • 56
  • 98