Button position change

I have one button in AbsoluteLayout in an XML file. From there, I can set the (x, y) button position.

How can I get and set the (x, y) button coordinates programmatically?

Thanks to everyone.

+6
source share
4 answers

You need to get a link to the button, for example, by calling findViewById () . When you received the link to the button, you can set the value of x and y with the button. setX () and button. setY () .

.... Button myButton = (Button) findViewById(R.id.<id of the button in the layout xml file>); myButton.setX(<x value>); myButton.setY(<y value>); .... 
+9
source

The answer you are looking for is in LayoutParams . Firstly, I would suggest not using AbsoluteLayout - it is deprecated - and using something else, perhaps FrameLayout, and just using the left and top margins as your x and y offsets.

However, to answer your question:

 Button button = (Button)findViewById(R.id.my_button); AbsoluteLayout.LayoutParams absParams = (AbsoluteLayout.LayoutParams)button.getLayoutParams(); absParams.x = myNewX; absParams.y = myNewY; button.setLayoutParams(absParams); 

Or alternatively:

 Button button = (Button)findViewById(R.id.my_button); button.setLayoutParams(new AbsoluteLayout.LayoutParams( AbsoluteLayout.LayoutParams.FILL_PARENT, AbsoluteLayout.LayoutParams, myNewX, myNewY)); 
+5
source

hummm u can try this. Is there an easy way to add a border to the top and bottom of the android view? Just go, drop this site, and get Sollution. If not, answer me. And if his help, then give me a vote. Thanks.

0
source

Source: https://habr.com/ru/post/885801/


All Articles