5

I've got an android widget, that displays a TextView in a FrameLayout. The TextView fills the whole parent layout. What I want to do, it be able to change at runtime where will the text will appear within the TextView.

Currently, I'd do it by changing the gravity on the TextView... It even has a setGravity(int) method, which should work perfectly with a RemoteView (I'm in a widget, remember?).. In theory anyway.

Unfortunately, this comes back with an error. Something about "cannot run setGravity(int) here" or sth. Will update with the exact error message if needed

I'm using this snippet:

myRemoteView.setInt(R.id.myTextView, "setGravity", myGravity);

Anyone tried this before, and has any ideas?

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
Artiom Chilaru
  • 11,811
  • 4
  • 41
  • 52

1 Answers1

2

It should work but, if not, maybe you could have two textviews and switch the visibility from GONE to VISIBLE to get the text at the right place.

Regards, Stéphane

Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • Well, that's what I ended up doing at the moment.. Unfortunately, having two of these textviews, with 3 gravity options means I have to manage 6 of them, with some nasty switch statements.. – Artiom Chilaru Jun 20 '11 at 11:58
  • And considering the fact that I also want to change the shadow color.. It's getting much much worse :( – Artiom Chilaru Jun 20 '11 at 11:59
  • Try to override the setGravity of a custom TextView class to see if it's called in the right way...could help... – Snicolas Jun 20 '11 at 12:02
  • 1
    Can't use custom classes in a RemoteView >_ – Artiom Chilaru Jun 20 '11 at 12:05
  • Considering nothing else came up, and I couldn't find anything betterm after tons of research - that is the best/only solution.. Accepted the answer ) – Artiom Chilaru Aug 24 '11 at 13:21
  • @ArtiomChilaru, you can use custom classes in `RemoteViews`. See [RemoteViews.RemoteView annotation](http://developer.android.com/reference/android/widget/RemoteViews.html#nestedclasses) – StenaviN May 14 '12 at 10:04
  • @StenaviN can that be used with android widgets as well? – Artiom Chilaru May 15 '12 at 16:39
  • 1
    @ArtiomChilaru, I don't know. Try yourself and share your experience. – StenaviN May 15 '12 at 18:36
  • Nope, seems like it's not possible after all. I just get errors stating that it can't load the class.. Seems like inside app widgets it can only load the classes defined within the framework itself – Artiom Chilaru Jun 24 '12 at 10:43