One more challenge was awaiting, I needed the center of the Relative Layout(root) to inflate the image dynamically at that location when admin wants to add new table in the floomap.
But the problem was – the height of Relative Layout Floormap(root) was not hardcoded. It adjusted according to screen size.
content_floormap.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:layout_weight="1"
tools:context="com.reservation.restaurant.Fragments.FragmentFloormap">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9"
android:id="@+id/root">
</RelativeLayout>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:background="#fff"
android:textColor="@color/colorPrimary"
android:layout_alignParentBottom="true"
android:text="Edit"
android:id="@+id/tableState"/>
</LinearLayout>
I solved the problem using ViewTreeObserver OnGlobalLayoutListener. Interface definition for a callback to be invoked when the global layout state or the visibility of views within the view tree changes.
Following is the code-
ViewTreeObserver vto = root.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
rootWidth = root.getWidth();
rootHeight = root.getHeight();
if (Build.VERSION.SDK_INT &amp;lt; 16) {
root.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
root.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
centerX = rootWidth/2;
centerY = rootHeight/2;
viewTablesOnScreen();
}
});
There’s lot of learning while we are stuck in a problem. Waiting for new challenge…. 😀.



