4

At the moment I am trying to set the position of my programmatically created view using the following code:

LayoutParams params = bottomBar.getLayoutParams();
params.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,(float) 5, getResources().getDisplayMetrics());
params.width = LayoutParams.MATCH_PARENT;
bottomBar.setLayoutParams(params);
bottomBar.setLeft(0);
bottomBar.setTop(this.getHeight()-bottomBar.getHeight());

THE PROBLEM

The error I get is that I cant use the setLeft and setTop properties in api levels less than 11.

THE QUESTION

How do I programmatically set the position of the view in API level < 11

StuStirling
  • 15,601
  • 23
  • 93
  • 150
  • The problem needs to do be outlined better, what are you trying to achieve? Maybe a simple layout diagram would help. If you want to align something to the bottom of the screen, think about relative layout using the equivalent of alignParentBottom="true" – Graham Smith Aug 13 '12 at 14:28
  • check out [this answer][1] , it straight forward [1]: http://stackoverflow.com/questions/12195768/android-use-of-view-setx-and-sety-in-api-8/28151869#28151869 – Orr Jan 26 '15 at 14:21

1 Answers1

2

It looks like you're already creating a custom view, so you would override onLayout() and call View#layout(int left, int top, int right, int bottom) on the layout you want.

final int left = 0;
final int top = getHeight() - bottomBar.getHeight();
final int right = left + bottomBar.getWidth();
final int bottom = top + bottomBar.getHeight();
bottomBar.layout(left, top, right, bottom);
DeeV
  • 35,865
  • 9
  • 108
  • 95