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.
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===========