56

I am developing a widget with an image on the rigth side, which can be choosen by the user in one of the settings screen. The image can then be set from the user with the ImageView.setScaleType("CENTER"). That works. The URL of the image is then stored in the preferences as URL, and also as Base64 encoded String of the Bitmap (cause I want to shrink the image and the user can rotate it in the settings)

In the widget, I load the image. That works fine. With the URI and with the Bitmap too. The ScaleType of the ImageView is set as well to a fixed value in the Layout, works too.

But how can I define the ScaleType of the ImageView in the widget programmatically? Cause I want to set the ScaleType to the value, the user has choosen in the settings. With RemoteViews, we cannot get the ImageView...

I tried:


myRemoteViews.setString(R.id.myImage, "setScaleType", "CENTER");


The logfile says: The function setScaleType is not possible for ImageView.

Does someone know how to make this definition inside the AppWidget?

user1013443
  • 798
  • 1
  • 8
  • 17

3 Answers3

81

You can use this segment to set scale type for Image View programatically..

 imgview.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

Hope it helps...

ShivRaj
  • 1,102
  • 6
  • 4
  • 16
    This is how to set the scale type for a normal `ImageView`, but the questioner was asking about in a `RemoteViews`. – Dan Hulme Feb 20 '13 at 10:08
9

If you look at the source code of ImageView, you can see the setScaleType method lacks the @android.view.RemotableViewMethod annotation, so it can't be called through the RemoteViews interface.

There's a workaround for this limitation, but it's not pretty: define a different layout XML for each ScaleType you want, and choose between them just before you create the RemoteViews. If your layout is complex, or you have more than one view to apply this workaround to, you might want to use RemoteViews.addView to add the particular ImageView layout inside the main layout.

I'm using this workaround (with addView) for appwidgets in my app Showr. The problem with this method is that the stock launcher has a bug: if you change the scale type of an existing appwidget by creating a new RemoteViews and calling addView to add a different ImageView layout to it, it doesn't always realise the layout has changed.

Dan Hulme
  • 14,779
  • 3
  • 46
  • 95
  • Not sure if this would work, but what about extending `ImageView` and adding the annotation, then your custom `ImageView` would call through to `super.setScaleType(...)` – pjco Apr 25 '13 at 15:15
  • 2
    You can't use your own classes in a `RemoteViews`, only the ones built into the system. – Dan Hulme Apr 25 '13 at 15:16
1

Since the is no simple way to call setScaleType on the remove view the work around I came up with was making three imageViews in my layout and then changing the visibility of the ones that don't have the scale type selected by the user:

    // first I declared a map to associate preference values with imageView ids

    private static final HashMap<Integer,Integer> widgetImageViewIds;

    static {
        widgetImageViewIds = new HashMap<Integer, Integer>();
        widgetImageViewIds.put(MyAppPreferences.PREF_ALIGN_START, R.id.imageViewStart);
        widgetImageViewIds.put(MyAppPreferences.PREF_ALIGN_CENTER, R.id.imageViewCenter);
        widgetImageViewIds.put(MyAppPreferences.PREF_ALIGN_END, R.id.imageViewEnd);
    }

    ...

    // then when I update my widget show/hide the widgets based on the preference

    for (HashMap.Entry<Integer, Integer> entry : widgetImageViewIds.entrySet()) {
        Integer imageViewAlignment = entry.getKey();
        Integer imageViewId = entry.getValue();
        views.setViewVisibility(imageViewId, imageViewAlignment.intValue() != imageAlignmentPreferenceValue ? View.GONE : View.VISIBLE);
    }
Code Commander
  • 16,771
  • 8
  • 64
  • 65