androidandroid-layoutandroid-graphics

Calculate Pixels from View to bottom of screen


I have a layout with a Spinner:

enter image description here

I would like to get the number of pixels from the top of the spinner to the bottom of the screen. Is there a way to achieve this?

enter image description here


Solution

  • To find the distance from top of the view, first find the distance of view from bottom and add the view height

    DisplayMetrics dm = new DisplayMetrics(); 
               getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
    int[] loc = new int[2];
    yourView.getLocationOnScreen(loc);
    int distance = dm.heightPixels - loc[1];
    Int viewHeight = yourView.getHeight()
    distance +=viewHeight; //distance from top of the view