0

I already tried the answer here: View getX() and getY() return 0.0 after they have been added to the Activity

private float foundHeight;
foundationZero = (ImageView) rootView.findViewById(R.id.foundation_zero);
        foundationZero.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // Encuentra la posición de las found para comenzar a posicionar los demás elementos
                foundHeight = foundationZero.getY();
                foundationZero.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                System.out.println("HEIGHT ---->: " + foundHeight);}
        });

XML where foundation is:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_fragment_view"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">
    <RelativeLayout
        android:id="@+id/gameInformationLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="@dimen/informationMargins"
            android:layout_marginTop = "@dimen/informationMargins"
            android:text="@string/scoreString"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop = "@dimen/informationMargins"
            android:text="@string/movString"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginTop = "@dimen/informationMargins"
            android:layout_marginRight="@dimen/informationMargins"
            android:text="@string/timeString"/>
    </RelativeLayout>

    <!-- Static Layout -->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp">

        <ImageView
            android:id="@+id/redeal_imageview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/foundationBetweenCardMargin"
            android:layout_marginLeft="@dimen/foundationBetweenCardMargin"
            android:adjustViewBounds="true"
            android:maxWidth="@dimen/maxWidthCard"
            android:maxHeight="@dimen/maxHeightCard"
            android:scaleType="centerCrop"
            android:src = "@drawable/b1fv"/>
        <ImageView
            android:id="@+id/foundation_zero"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/foundationBetweenCardMargin"
            android:layout_marginRight="@dimen/foundationBetweenCardMargin"
            android:adjustViewBounds="true"
            android:maxWidth="@dimen/maxWidthCard"
            android:maxHeight="@dimen/maxHeightCard"
            android:scaleType="centerCrop"
            android:layout_toStartOf="@+id/foundation_one"
            android:layout_toLeftOf="@+id/foundation_one" />
        <ImageView
            android:id="@+id/foundation_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/foundationBetweenCardMargin"
            android:layout_marginRight="@dimen/foundationBetweenCardMargin"
            android:adjustViewBounds="true"
            android:maxWidth="@dimen/maxWidthCard"
            android:maxHeight="@dimen/maxHeightCard"
            android:scaleType="centerCrop"
            android:layout_toStartOf="@+id/foundation_two"
            android:layout_toLeftOf="@+id/foundation_two" />
        <ImageView
            android:id="@+id/foundation_two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/foundationBetweenCardMargin"
            android:layout_marginRight="@dimen/foundationBetweenCardMargin"
            android:adjustViewBounds="true"
            android:maxWidth="@dimen/maxWidthCard"
            android:maxHeight="@dimen/maxHeightCard"
            android:scaleType="centerCrop"
            android:layout_toStartOf="@+id/foundation_three"
            android:layout_toLeftOf="@+id/foundation_three" />
        <ImageView
            android:id="@+id/foundation_three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:adjustViewBounds="true"
            android:maxWidth="@dimen/maxWidthCard"
            android:maxHeight="@dimen/maxHeightCard"
            android:scaleType="centerCrop"
            android:layout_marginRight="@dimen/foundationBetweenCardMargin" />


    </RelativeLayout>


</FrameLayout>

In returns 0.0. I need the position of those image views because I'm adding more views programmatically. As can be seen in the code I already tried the solution in the URL but it doesn't work. By the way, I added "used in fragment" in the title because the fact that the above code is written in a Fragment maybe has to do with the fact that it didn't work.

Miguel Duran Diaz
  • 302
  • 1
  • 3
  • 12

1 Answers1

0

View.getY() does not give you the height of a view, but rather the Y position of the top left corner of the view inside the container. Instead, you want to use View.getHeight(), which will return you the height of the view in pixels (NOT DP!).

Elli White
  • 1,440
  • 1
  • 12
  • 21