3

I am designing (trying to learn) a simple widget for Android. I have:

public class NewAppWidget extends AppWidgetProvider {

Inside that class, I have the simple code:

public double angle(Calendar calendarDate) {
        //Earth rotation;
        int day = calendarDate.get(Calendar.DAY_OF_YEAR);
        int angle;
        angle = (day / 365) * 360;
        return angle;
    }

I then want to select an image and rotate it by that angle in ImageView, which totally does not work ...

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                            int appWidgetId) {



 // Construct the RemoteViews object

 ImageView image = (ImageView) findViewById(R.layout.new_app_widget);

    image.setImageResource(R.drawable.moon10);
    image.setRotation(angle);

    // Instruct the widget manager to update the widget
    appWidgetManager.updateAppWidget(appWidgetId, new_app_widget);

}

There are probably several mistakes in there - I am a beginner.

The three errors highlighted by Android studio are:

  1. findViewById in red (cannot resolve method)
  2. angle in red (cannot resolve symbol angle)
  3. new_app_widget in last line in red (cannot resolve symbol)

Thanks for your help and have a good day.

JY

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Jean-Yves
  • 31
  • 3
  • Note: For your title 1) Android Studio is just an IDE, it has nothing to do with the question. 2) Android code is (mostly) Java, so you don't need to say "Java code" – OneCricketeer Feb 12 '16 at 07:45
  • 1
    I'm voting to reopen this question because it is not a duplicate: it is not about rotating a "normal" View because OP wants to rotate an ImageView which is part of a RemoteViews, where (see my answer) "View.setRotation()" is not possible. OP accepted the "duplicate" mostly out of inexperience but future readers looking for a solution involving app widgets (home screen widgets) should not be directed to the linked post but to this one. – Bö macht Blau Sep 19 '17 at 17:46

3 Answers3

0
  1. as the method is static, you will need to create view to findviewbyid(); or make void non-static
  2. angle in rotation must be int, not double
theMatus
  • 153
  • 1
  • 9
0

For getting Orientation, use below function. Just pass context and Uri of Image Path

public static int getGalleryOrientation(Context context, Uri path) {
    /* it's on the external media. */
    Cursor cursor = context.getContentResolver().query(path,
            new String[]{MediaStore.Images.ImageColumns.ORIENTATION},
            null, null, null);

    if (cursor.getCount() != 1) {
        return -1;
    }

    cursor.moveToFirst();
    return cursor.getInt(0);

}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
jigspatel
  • 400
  • 3
  • 14
0

TL;DR

It is not possible to use the method View.setRotation(float) with any View which is part of a home screen widget, at least that's what my IDE tells me:

W/AppWidgetHostView﹕ updateAppWidget couldn't find any view, using error view android.widget.RemoteViews$ActionException: view: android.widget.ImageView can't use method with RemoteViews: setRotation(float)

That being said, let's at least get your app widget up and running :)

You need a layout file with an ImageView, let's call it new_app_widget.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="@dimen/widget_margin"
            android:background="#09C">
    <ImageView
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:background="#ffffff"
        android:id="@+id/imageView"
        android:layout_gravity="center"
       />

</FrameLayout>

Change your code like this:

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                        int appWidgetId) {

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

    // use the RemoteViews methods to access the children of the FrameLayout:
    views.setImageViewResource(R.id.imageView, R.drawable.moon10);


    // Instruct the widget manager to update the widget
    appWidgetManager.updateAppWidget(appWidgetId, views);

}

If you need some rotation, you will have to perform it on the Drawable and then with the rotated Drawable call setImageViewResource() like above. See also Rotating a drawable in Android

Finally, about:

angle in red (cannot resolve symbol angle)

You have defined a method angle(Calendar). If you want to use it, you need to call it with some Calendar input.

Community
  • 1
  • 1
Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
  • 1
    Ah, I assumed ImageView was just a regular View, my bad! – OneCricketeer Feb 12 '16 at 09:06
  • While I read we should avoid comments like "Thanks", I can't help but must say it! It works totally, problem solved, great week end ahead. – Jean-Yves Feb 12 '16 at 13:17
  • @Jean-Yves - thanks for letting me know :) You can indicate the answer solved your problem by clicking the checkmark at the top left corner of the answer. Of course, you can eventually upvote it as well. This will on the one hand result in some "reputation points" for me. On the other hand, the pair (question and answer) will be easier to find for people searching for a solution to their own problem in the future. If the "duplicate" state prevents any of these actions, well, never mind. This was fun :D – Bö macht Blau Feb 12 '16 at 15:19
  • Hi, my reputation is too low to upvote you ... sorry but I will get there in time ... may be ... – Jean-Yves Feb 15 '16 at 08:23
  • @Jean-Yves Just keep asking questions like this (or writing an answer now and then) and you'll do fine ;) But honestly, never mind the upvote. On *stack overflow* it's like in real life: there are things which are fun to do. A little rep now and then doesn't hurt. But a nice puzzle or a question which leads me to discovering something new is even better :) – Bö macht Blau Feb 15 '16 at 08:39