0

Good day! I'm having a difficulty getting the coordinated of some controls of mine. I would like to clarify that the controls are located inside a fragment displayed in a parent activity. I need this to run on LinearLayout.

I have read that the problem lies in the order that we execute the retrieval, so I did try to use view.post(new Runnable) and getTranslationX() and it yielded the same result.

other references would be appreciated. Thank you.

  1. getHeight returns 0 for all Android UI objects

  2. How to retrieve the dimensions of a view?

  3. View getX() and getY() return 0.0 after they have been added to the Activity

EDIT: I've found something quite alarming. I decided why not try attaching the code to a button and see if we can get any values, and when I did and ran the app then pressed the button, I got 0! What exactly is going on?

EDIT: Changed API level from 9 to 11. Yields same result.

Here is the code:

//this class extends fragment
    @SuppressLint("NewApi")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.game_sh_view, container, false);

        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //1.    Fetch the controls
                getControls();

                //2.    Add function to the controls
                setControlFunctions();

                //3.    Execute on Init settings
                getScreenSize();
                locateControls();
                threadingMethods();

                //4.    Misc onInit commands
                onInitAesthetics();
            }
        });

        return view;
    }

    float screenSizeX;
    float screenSizeY;
    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB_MR2)
    public void getScreenSize(){
        WindowManager wm = getActivity().getWindowManager();
        Display disp = wm.getDefaultDisplay();
        Point size = new Point();
        disp.getSize(size);
        screenSizeX = size.x;
        screenSizeY = size.y;
        sop("=============SCREEN SIZE==============");
        sop("Screen Size X: "+size.x);
        sop("Screen Size Y: "+size.y);
        sop("============SCREEN SIZE END===========");
    }
    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB_MR2)
    public void locateControls(){
        comLeftYLocation = comLeft.getY();
        comLeftYPermLocation = comLeft.getY();
        sop("=============comLeft LOCATOR==============");
        sop("comLeft Location Y: "+comLeft.getY());
        sop("============comLeft LOCATOR END===========");
    }

XML Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1"
    tools:context="layout.game_sh"
    android:orientation="vertical">

    <!-- TODO: Update blank fragment layout -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="30dp"
        android:orientation="horizontal"
        tools:ignore="UselessParent">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/wood"
            app:srcCompat="@drawable/h_f"
            tools:ignore="ContentDescription" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/wood"
            app:srcCompat="@drawable/h_f"
            tools:ignore="ContentDescription" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/wood"
            app:srcCompat="@drawable/h_f"
            tools:ignore="ContentDescription" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/wood"
            app:srcCompat="@drawable/h_f"
            tools:ignore="ContentDescription" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/wood"
            app:srcCompat="@drawable/h_f"
            tools:ignore="ContentDescription" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/comLeft"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            app:srcCompat="@drawable/hand_left_down"
            tools:ignore="ContentDescription"
            tools:layout_editor_absoluteX="103dp"
            tools:layout_editor_absoluteY="-13dp" />

        <ImageView
            android:id="@+id/comRight"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            app:srcCompat="@drawable/hand_right_down"
            tools:ignore="ContentDescription"
            tools:layout_editor_absoluteX="191dp"
            tools:layout_editor_absoluteY="8dp" />
    </LinearLayout>

    <Space
        android:layout_width="match_parent"
        android:layout_height="100dp"
        tools:ignore="NewApi" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/playerLeft"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/hand_left_up"
            tools:ignore="ContentDescription" />

        <ImageView
            android:id="@+id/playerRight"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/hand_right_up"
            tools:ignore="ContentDescription" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        tools:ignore="UselessParent">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/wood"
            app:srcCompat="@drawable/h_f"
            tools:ignore="ContentDescription" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/wood"
            app:srcCompat="@drawable/h_f"
            tools:ignore="ContentDescription" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/wood"
            app:srcCompat="@drawable/h_f"
            tools:ignore="ContentDescription" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/wood"
            app:srcCompat="@drawable/h_f"
            tools:ignore="ContentDescription" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/wood"
            app:srcCompat="@drawable/h_f"
            tools:ignore="ContentDescription" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">

        <Button
            android:id="@+id/l_trigger"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lefttrigger"
            tools:ignore="ButtonStyle" />

        <Button
            android:id="@+id/r_trigger"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/righttrigger"
            tools:ignore="ButtonStyle" />
    </LinearLayout>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/textview" />

</LinearLayout>

The result in my console:

[ 10-08 23:27:39.511 30284:30406 W/         ]
                                                                           fimg library or stretchFimgApi_v5 symbol can not be loaded
10-08 23:27:39.521 30284-30284/com.example.dale.finalproject I/System.out: =============SCREEN SIZE==============
10-08 23:27:39.521 30284-30284/com.example.dale.finalproject I/System.out: Screen Size X: 1080
10-08 23:27:39.521 30284-30284/com.example.dale.finalproject I/System.out: Screen Size Y: 1920
10-08 23:27:39.521 30284-30284/com.example.dale.finalproject I/System.out: ============SCREEN SIZE END===========
10-08 23:27:39.521 30284-30284/com.example.dale.finalproject I/System.out: =============comLeft LOCATOR==============
10-08 23:27:39.521 30284-30284/com.example.dale.finalproject I/System.out: comLeft Location Y: 0.0
10-08 23:27:39.521 30284-30284/com.example.dale.finalproject I/System.out: ============comLeft LOCATOR END===========
  • What controls? What is `comLeft`? Is it the `LinearLayout`? If not, where is it? I would mention that `getX()` and `getY()` return a `View`'s x/y with respect to its immediate parent. – Mike M. Oct 09 '17 at 03:34
  • comLeft is an ImageView that is within a LinearLayout within another Linear Layout. – Bumpy Oct 09 '17 at 08:57
  • ^ sorry I was using my stack exchange app and facebook login option was not available. – james castillo Oct 09 '17 at 09:00
  • What is the actual layout? That one opening tag doesn't really tell us anything at all. Furthermore, why do you expect the coordinates to be non-zero? As I mentioned above, those methods return a `View`'s coordinates with respect to its direct parent `ViewGroup`. If Bumpy's description is accurate, and the `ImageView` is the first child of the `LinearLayout`, it's very likely that it's at (0, 0). – Mike M. Oct 09 '17 at 14:30
  • What do you mean by actual layout? I'm sorry but I'm still trying to get used to android since it is way different than java in terms of GUI. Anyways, I updated the XML to give everyone more information. – james castillo Oct 10 '17 at 11:28
  • Yeah, that's what I meant. I shoulda said "complete" instead of "actual". Anyway, the `comLeft` `ImageView` is the first child of its parent `LinearLayout`, and there are no margins, paddings, or gravity settings to affect it, so it's aligned to the parent's top-left corner, and therefore is at (0, 0). That is, you should be getting 0 for `getY()`. If you want the position with respect to the root `ViewGroup`, you could traverse up the hierarchy, summing the coordinates 'til you get to the root. Or maybe the `View#getLocationInWindow()` or `getLocationOnScreen()` methods would suffice for you. – Mike M. Oct 10 '17 at 14:35

0 Answers0