I'm trying to add a view programmatically and set it to a specific location on the screen.
Adding the view:
View view = new View(this);
Then I'm adding a GlobalLayoutListener
ViewTreeObserver observer = someLayout.getViewTreeObserver();
And finally in onGlobalLayout I'm setting its location:
public void onGlobalLayout() {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, height);
layoutParams.setMargins((int) (0.81 * width), (int) (someLayout.getY() + someLayout.getHeight() - (view / 2)), 0, 0);
view.setLayoutParams(layoutParams);
}
I've also tried using ViewGroup.MarginLayoutParams
Using API 24 and above this works well, but API 23 (and lower) ignores the margins and the view is placed on the top left corner of the screen.
How can this issue be solved to support all API levels?
from LayoutParams setMargins not working on API 23 and lower
No comments:
Post a Comment