Milos Cuculovic
Milos Cuculovic

Reputation: 20223

Set an item position in the view programmatically with the x and y coordinates

I have an ImageButton and I would like to set its position programmatically to x and y.

How can I do this? I read something abaut setTop() and setLeft() but don't know how to use it.

Upvotes: 0

Views: 1482

Answers (1)

Karthi
Karthi

Reputation: 13752

AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(
         int width, int height, int x, int y);
params.x=30;
params.y=50;

imageButton.setLayoutParams(params);

using this code you can position you item in absolute (x,y) coordinate in your view, but you need to use you main root layout AbsoluteLayout otherwise it throws error.

But AbsoluteLayout is depreciated in latest version so avoid that , try to use RelativeLayout

Upvotes: 2

Related Questions