Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android set view position - setY vs setTop

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>
like image 408
Castor Avatar asked Sep 30 '16 16:09

Castor


1 Answers

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 the y value passed in and the current top property.

setTop()

Sets the top position of this view relative to its parent.

like image 64
technophyle Avatar answered Oct 07 '22 20:10

technophyle