Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

understanding View.setTranslationY() [ /X() ]

as the title says i want to understand what that method exactly is doing.

firstable i have double checked that the coordinate system of android works like that: coordinate system

secondary - please take a minute to check my android studio screen, and the method quick doc. why is that value of my view (after clicking it) is 106? android screen

like image 338
Itay Cohen Avatar asked Jun 08 '17 11:06

Itay Cohen


People also ask

How do I find X and Y coordinates on Android?

getX() + ":" + (int)button1. getY(), Toast. LENGTH_LONG). show();

What is Translationx?

the translation is an offset that is applied to the position of the View . if the layout place your View at x;y and you call setTranslationY(10) , your View will appear at x;y+10 . it's a way to control the positioning of the View after the layout. bonus tip, instead of logging everything, use the debugger.

What does findViewById return?

findViewById returns an instance of View , which is then cast to the target class. All good so far. To setup the view, findViewById constructs an AttributeSet from the parameters in the associated XML declaration which it passes to the constructor of View . We then cast the View instance to Button .


1 Answers

the coordinate system is correct.

getY() will return the value of the top of your View + the Y translation. so, if getY() is returning 106 and you set translation y to 10, the top of your view should be at 96. try calling also getTop() and check what is that value

the translation is an offset that is applied to the position of the View. if the layout place your View at x;y and you call setTranslationY(10), your View will appear at x;y+10. it's a way to control the positioning of the View after the layout

bonus tip, instead of logging everything, use the debugger

in case you still have doubts about the difference between the position and translation, you could try this, create an empty activity and set this layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.lelloman.dummy.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="asd asd"
        android:layout_above="@+id/button"
        android:layout_centerHorizontal="true"/>
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="asd"/>

</RelativeLayout>

then, you will see that the TextView is right above the Button, because this is how the RelativeLayout will position the Views given these layout parameters. now, try to call

findViewById(R.id.button).setTranslationY(100);

you will notice that the button will be moved down by 100px, but the TextView will still be at the old position, because the translation is applied after the layout. it is something specific to that View that is not taken into account for the positioning of the View within its parent

you could also set the translation in the xml with

<Button
    android:translationY="100px"
    ...
like image 78
lelloman Avatar answered Sep 28 '22 08:09

lelloman