get View Dimension - Android User Interface

Android examples for User Interface:View

Description

get View Dimension

Demo Code


//package com.java2s;
import android.content.Context;

import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

public class Main {

    public static int[] getViewDimension(View view) {
        ViewGroup.LayoutParams lotParams = view.getLayoutParams();

        int width = lotParams.width;
        if (width <= 0) {
            width = ((WindowManager) view.getContext().getSystemService(
                    Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
        }//from  w  ww  .ja va  2 s . c om

        int height = lotParams.height;
        if (height <= 0) {
            height = ((WindowManager) view.getContext().getSystemService(
                    Context.WINDOW_SERVICE)).getDefaultDisplay()
                    .getHeight();
        }

        return new int[] { width, height };
    }
}

Related Tutorials