Search code examples
androidandroid-scrollviewandroid-relativelayout

Relative layout changes the coordinates of imageview


I am using an imageView of a map in my app, the map needs to be scrolled, and I have used two horizontal and vertical scrollview for that, but the relative view in the scrollview manipulates the coordinates of the picture so that the String xValue = String.valueOf(event.getX()); return a large x such as 3200 in an image with dims of 400*250! but the y value returned is correct, using linear layout causes a problem described in this link. (The outer Relative View causes this issue)

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="100dp"
    android:layout_marginTop="150dp"
    android:scrollbarStyle="outsideOverlay"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:id="@+id/relative_mother"
        android:background="#000000"
        android:padding="3dp">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbarStyle="outsideOverlay"
            android:foregroundGravity="center"
            android:background="#F0F0B0"
            android:id="@+id/map_scrollview"
            android:paddingLeft="32dp"

            >
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingTop="32dp"
                android:paddingLeft="220dp"
                android:paddingRight="12dp">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:srcCompat="@android:drawable/ic_input_add"
                    android:id="@+id/imageView2"
                    android:gravity="center_vertical|center_horizontal"/>
            </LinearLayout>
        </ScrollView>

    </RelativeLayout>
</HorizontalScrollView>

Solution

  • I think giving gravity to linear layout same as image view might help, and try not to use nested scroll view, prefer recyclerview or something else instead.