get View Top Including Margin - Android User Interface

Android examples for User Interface:View Margin

Description

get View Top Including Margin

Demo Code


//package com.java2s;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class Main {
    public static int getTopIncludingMargin(View view) {
        return view.getTop() - getTopMargin(view);
    }/* w w w.j  av  a 2  s.co  m*/

    public static int getTopMargin(View view) {
        if (view.getLayoutParams() instanceof LinearLayout.LayoutParams) {
            return ((LinearLayout.LayoutParams) view.getLayoutParams()).topMargin;
        } else if (view.getLayoutParams() instanceof RelativeLayout.LayoutParams) {
            return ((RelativeLayout.LayoutParams) view.getLayoutParams()).topMargin;
        } else if (view.getLayoutParams() instanceof FrameLayout.LayoutParams) {
            return ((FrameLayout.LayoutParams) view.getLayoutParams()).topMargin;
        }
        return 0;
    }
}

Related Tutorials