0

I am trying to create a widget which has one ImageView. I want it to work like a toggle button. One a click, it should do one thing and, on another click do a different thing, and repeat.

I have stumbled a lot of tutorials on widget and many posts on stackoverflow, but no success so far. I want to do something like: On first click, start a process, change the ImageView and wait for another click to stop. I have seen a widget like this

My code so far:

private Boolean clicked = false;

...

// Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);

        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_camera);
        views.setImageViewBitmap(R.id.imageView, bitmap);

        Intent intent1 = new Intent(context, MainActivity.class);
        Intent intent2 = new Intent(context, MainActivity2.class);

        PendingIntent firstPIntent = PendingIntent.getActivity(context, 0, intent1, 0);
        PendingIntent secondPIntent = PendingIntent.getActivity(context, 0, intent2, 0);

        if(!clicked)
        {
            views.setOnClickPendingIntent(R.id.imageView, firstPIntent);
            clicked = true;
            Log.e("WIdGEt", "ONE");
        }
        else if(clicked)
        {
            views.setOnClickPendingIntent(R.id.imageView, secondPIntent);
            clicked = false;
            Log.e("WIdGEt", "TWO");
        }

Please help me with some code (or pseudocode). Thanks!

TheOnlyAnil
  • 877
  • 1
  • 15
  • 27
  • 2
    Please explain **exactly** what "no success so far" means. You posted code. What is the behavior when you run your code? Also, please bear in mind that `clicked` is useless, as your `AppWidgetProvider` is only used for one update call. You need to track the `clicked` state in a file, database, or `SharedPreferences`. – CommonsWare Jul 30 '15 at 19:07
  • It just runs the firstPIntent. I want to do something like: On first click, start a process, change the ImageView and wait for another click to stop. I have seen a widget like this. – TheOnlyAnil Jul 30 '15 at 19:16
  • In addition to what @CommonsWare suggested, I'd add that this `R.mipmap.ic_camera` doesn't seem to me a good practice, since `mipmap` should only be used for the application icon, AFAIK. – Phantômaxx Jul 30 '15 at 19:17
  • @DerGol...lum Yes, it's not a good idea if I want to stretch the widget. My widget is 1x1 cell. So mipmap icon is fine. – TheOnlyAnil Jul 30 '15 at 19:20

1 Answers1

4

It just runs the firstPIntent.

That is because clicked is always false, because clicked is (apparently) a field in your AppWidgetProvider, and an AppWidgetProvider instance only lives for one update.

You need to adjust your logic to track your "clicked" status in a file, database, or SharedPreferences — something that will last not only after the AppWidgetProvider instance is gone, but something that will be around after your process is terminated.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for your answer. My basic requirement is: On first click, start a process, change the ImageView and wait for another click to stop. I have some questions... 1. Do I need to create onReceive method for this? 2. Is one class enough or there should be two classes for "start" and "stop"? 3. So sharedpreferences should be in MainActivity.class, right? Sorry for so many questions, they might sound amateurish as I am new to Android development. – TheOnlyAnil Jul 30 '15 at 19:26
  • 1
    @TheOnlyAnil: "1. Do I need to create onReceive method for this?" -- I have no idea. Right now, your `PendingIntent` objects are pointing to activities. If you want the app widget UI to change without displaying an activity, you could have the `PendingIntent` point back to your `AppWidgetProvider`. That would require you to override `onReceive()`, identifying whether it is one of your app widget clicks or not, and letting `AppWidgetProvider` handle the "or not" case. – CommonsWare Jul 30 '15 at 19:45
  • @TheOnlyAnil: "2. Is one class enough or there should be two classes for "start" and "stop"?" -- one would seem to be sufficient. "3. So sharedpreferences should be in MainActivity.class, right?" -- well, everything that needs to know about your "clicked" state would need to read (and possibly write) this `SharedPreferences` value. – CommonsWare Jul 30 '15 at 19:46