I was going to move the button's position programmatically. Button is in relative layout. I'd researched and found that we can use .setY()
or .setTop()
. It looks like they should work the same.
But in my case, .setTop()
does not change the position at all and .setY()
works only. I'm not sure what I did misunderstand but it's very weird for me.
Is there anybody who can explain setY()
vs setTop()
correctly?
What is the difference?
This is layout.xml
:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
</RelativeLayout>
The main difference between setY()
and setTop()
is that setY()
sets the top offset of the view relative to the visual area, whereas setTop()
sets the top offset of the view relative to its parent.
From the Android documentation.
setY()
Sets the visual y position of this view, in pixels. This is equivalent to setting the
translationY
property to be the difference between they
value passed in and the currenttop
property.
setTop()
Sets the top position of this view relative to its parent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With