10

As shown in below image. I do have one relative layout. I have my ImageView in same and I need to get gap in pixels between view starts and this ImageView. My activity is as below. Please advice. I tried various ways like topY() but couldn't find solution.

Find distance in pixcel

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/main_ruler_linear_layout"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:orientation="horizontal" 
        >        
        </LinearLayout>

    <ImageView 
        android:id="@+id/line_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/line"/>

    <EditText
        android:id="@+id/txtRulerCenterPosition"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/main_ruler_linear_layout"
        android:layout_marginRight="17dp"
        android:layout_marginTop="16dp"
        android:ems="10" 
        android:textSize="5dp"
        android:focusable="false"/>

</RelativeLayout>
Irshu
  • 8,248
  • 8
  • 53
  • 65
Ketan Bhavsar
  • 5,338
  • 9
  • 38
  • 69

2 Answers2

14

To calculate the distance between the bottom of the one view and the top of the other view:

View oneView = ...;
View otherView = ...;

RectF oneRect = calculateRectOnScreen(oneView);
RectF otherRect = calculateRectOnScreen(otherView);

float distance = Math.abs(oneRect.bottom - otherRect.top);

The util method:

public static RectF calculateRectOnScreen(View view) {
    int[] location = new int[2];
    view.getLocationOnScreen(location);
    return new RectF(location[0], location[1], location[0] + view.getMeasuredWidth(), location[1] + view.getMeasuredHeight());
}

In some cases you need to replace the view.getLocationOnScreen to view.getLocationInWindow.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
2

if both views are on same level in view hierarchy then you can use following method to determine vertical distance between them in pixels.

public static int getDistanceBetweenViews(View firstView, View secondView) {
        int[] firstPosition = new int[2];
        int[] secondPosition = new int[2];

        firstView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        firstView.getLocationOnScreen(firstPosition);
        secondView.getLocationOnScreen(secondPosition);

        int b = firstView.getMeasuredHeight() + firstPosition[1];
        int t = secondPosition[1];
        return Math.abs(b-t);
    }
Zain Ali
  • 15,535
  • 14
  • 95
  • 108