0

I have a widget whose xml layout is simple: an ImageView and a TextView.

I can hardcode the rotation of the TextView in the xml by using android:rotation.

However I want to be able to set the rotation of the TextView programmatically. It seems that a View has a setRotation() method, so the TextView will inherit this method, such that a "normal" TextView can be rotated programmatically used this method.

But when the TextView is buried within a RemoteViews, you have to call the methods indirectly, like setTextViewText() and setTextViewTextSize().

And it also seems that you can call the general setFloat(int viewId, String methodName, float value) to set the value of any methodName, so you can set text size also via passing "setTextSize" to setFloat().

OK to the question....

Since there isn't a setTextViewRotation() method in RemoteViews, I figure I need to call that indirectly using setFloat(viewId, "setRotation", value).

But when I try that, my widget just shows a "Problem Loading Widget" message box.

It works with e.g. setFloat(viewId, "setTextSize", value) to change the text size, so I'm on the right track, but it doesn't work for setRotation in the same place in my code.

I'm guessing that it's because setRotation is an inherited method (from View), rather than a method of TextView itself, but it leaves me slightly stuck as to how to rotation my TextView.

Any ideas?

Thanks!

drmrbrewer
  • 11,491
  • 21
  • 85
  • 181

1 Answers1

3

The reason why you are crashing is because setRotation() does not have the @RemotableViewMethod annotation in the Android source code, and therefore it cannot be set using setFloat().

I am not aware of any way for you to change the rotation property of a View dynamically in an app widget. One workaround is to support N rotations via N versions of your layout file, each of which has a hardcoded android:rotation value.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • oooh that's a shame. Could I have N TextViews in *one* layout with N different rotations, and set the visibility of only the one I want to Visible? Even if that is possible, is having multiple versions of the layout file more elegant (I'm not even sure how to go about that)? – drmrbrewer Jan 19 '15 at 18:43
  • @drmrbrewer: "Could I have N TextViews in one layout with N different rotations, and set the visibility of only the one I want to Visible?" -- you can give that a try, though you'll have to remember which of the N `TextViews` is the one that you are using, so you can set the text on it (now and in updates). "is having multiple versions of the layout file more elegant" -- well, both are nasty hacks. :-) "I'm not even sure how to go about that" -- create `layout0.xml`, `layout90.xml`, etc. for different degrees, and use the right `R.layout` value in your `RemoteViews` constructor. – CommonsWare Jan 19 '15 at 18:45
  • @drmrbrewer: "is having multiple versions of the layout file more elegant" -- ignoring elegance, if you are aiming for just a few different rotation values, your approach of N `TextViews` is simpler. However, if you wanted 360 different rotations, having 360 `TextView` widgets in an app widget would be unkind to the home screen implementation, as its process is the one whose heap you're consuming. – CommonsWare Jan 19 '15 at 18:47
  • Thanks for the further comments. On reflection, it seems to me that your suggestion of multiple layouts is probably the neatest (or the most desirable of the nasty hacks :-) since I can just choose the appropriate layout when constructing the RemoteViews, and then the remainder of the code (including referencing the TextView id) is completely unchanged. Maybe if my layout was complex, with the TextView being just a small component, then a single layout and multiple TextViews would have been neater. Anyhow, got it working now, so thanks :) – drmrbrewer Jan 19 '15 at 22:01