8

Dear all, I am trying to set the position of a TextView within an appwidget.

Basically, the direct access to properties af the TextView works:

myRemoteView.setTextColor(R.id.myTextView, Color.WHITE);   //works

Also indirectly, I can access the TextView properties:

myRemoteView.setInt(R.id.myTextView, "setTextColor", Color.BLUE); // works

And setting float values also works:

myRemoteView.setFloat(R.id.myTextView, "setTextSize", 25); // works

Now I'm trying to shift the x position of the TextView by 5 pixels:

myRemoteView.setFloat(R.id.myTextView, "setTranslationX", 5); // does not work
myRemoteView.setFloat(R.id.myTextView, "setX", 5); // does not work
myRemoteView.setInt(R.id.myTextView, "setLeft", 5); // does not work

I tried FrameLayout and RelativeLayout, and also AbsoluteLayout (depreciated). Nothing works. But there must be a way to do that. Can you please help?

Oliver
  • 81
  • 3

2 Answers2

0
At the time of the answer, the RemoteViews API does not allow you to set translation or position

However, you can achieve the desired effect by setting the padding for the parent of your TextView
RemoteViews#setViewPadding(int, int, int, int, int, int)

Or, since API 31, you can also set the margin for your TextView
RemoteViews#setViewLayoutMargin(int, int, float, int)

vadiole
  • 337
  • 1
  • 6
  • 21
-4
LayoutParams lp = myRemoteView.getLayoutParams();
lp.marginLeft = 5; 
myRemoteView.setLayoutParams(lp);
hunterp
  • 15,716
  • 18
  • 63
  • 115