get View Width - Android User Interface

Android examples for User Interface:View Size

Description

get View Width

Demo Code


//package com.java2s;
import java.lang.reflect.Method;
import android.view.View;
import android.view.View.MeasureSpec;

public class Main {
    public static int getViewWidth(View v) {
        try {// w ww. j av  a2  s. c  o m
            Method m = v.getClass().getDeclaredMethod("onMeasure",
                    int.class, int.class);
            m.setAccessible(true);
            m.invoke(v, MeasureSpec.makeMeasureSpec(
                    MeasureSpec.UNSPECIFIED, 0), MeasureSpec
                    .makeMeasureSpec(
                            ((View) v.getParent()).getMeasuredWidth(),
                            MeasureSpec.AT_MOST));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return v.getMeasuredWidth();
    }
}

Related Tutorials