0

I am writing a method that draws a layout ontop of buttons. My problem is with the position of the layout that I am creating on lower API's. On newer I am using the setX() and setY() methods but since that is not working on lower I tried with setting the layout params as described here Android - Use of view.setX() and setY in api 8 but I am not getting the results I want. With this

int[] location = new int[2];
button.getLocationOnScreen(location);
int x = location[0];
int y = location[1];
borderRelativeLayout.setX(x);
borderRelativeLayout.setY(y);
viewGroup.addView(borderRelativeLayout)

I am achieving this: correct position

but if I use this code to support lower API's

relativeLayoutparams.leftMargin = x;
relativeLayoutparams.topMargin = y;
viewGroup.addView(borderRelativeLayout, relativeLayoutparams);

I am getting this as a result: incorrect

Any help would be appreciated.

Community
  • 1
  • 1
Alex
  • 23
  • 8

1 Answers1

0

For API 1:

int[] location = new int[2];
button.getLocationOnScreen(location);
int x = location[0];
int y = location[1];
borderRelativeLayout.offsetLeftAndRight(x - borderRelativeLayout.getLeft());
borderRelativeLayout.offsetTopAndBottom(y - borderRelativeLayout.getTop());

EDIT:
Did you consider a background drawable for desired effect?

Milos Fec
  • 828
  • 6
  • 11
  • Thanks, but `setLeft` and `setTop` for relative layout also requires api 11. What do you mean by using background drawable? Can you be more spesific? – Alex Dec 18 '15 at 12:00
  • Sorry, I didn't realized that. You can use offsetLeftAndRight and offsetTopAndBottom. Anyway, what about the drawables? You can set the border using drawable. – Milos Fec Dec 18 '15 at 12:03
  • I am using drawable to create the border on the layout. And I am using a layout to do this and not directly to the button, cause I don't want to change the style of a button if it's a custom one. I will try your method with offset now. Update: same results. – Alex Dec 18 '15 at 12:16
  • Why are you positioning the layout manually? Do you have one RelativeLayout with the border drawable for every button? – Milos Fec Dec 18 '15 at 12:20
  • Yes. I am creating them dynamically. Whenever I detect a button in a view, I place a border around it. – Alex Dec 18 '15 at 12:22
  • If you have to do it this way, you can create FrameLayout with the border, get layoutParams from the Button, remove the Button from the layout and add it to new FrameLayout (setPadding so you will see the border), then add the FrameLayout to original layout with original Button's layoutParams. Better will be to use custom ViewGroup for the buttons and override the addView method, where you can set the border. – Milos Fec Dec 18 '15 at 12:26
  • Positioning views manually doesn't look like a good idea (when not using custom ViewGroup). Also having so many layouts could cause performance issues. – Milos Fec Dec 18 '15 at 12:29
  • Maybe better way will be to override onDraw method of your layout holding all buttons. Than, after every button is drawn, you can iterate through all buttons and draw border around them. – Milos Fec Dec 18 '15 at 12:32
  • Well I can't override onDraw cause I don't know the layout cause I am writing this method for a library. But thanks for all the suggestions. – Alex Dec 18 '15 at 14:50