Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access layout_height from within my custom view?

I have a custom view and I simply wish to access the xml layout value of layout_height.

I am presently getting that information and storing it during onMeasure, but that only happens when the view is first painted. My view is an XY plot and it needs to know its height as early as possible so it can start performing calculations.

The view is on the fourth page of a viewFlipper layout, so the user may not flip to it for a while, but when they do flip to it, I would like the view to already contain data, which requires that I have the height to make the calculations.

Thanks!!!

like image 634
Brad Hein Avatar asked Aug 22 '10 17:08

Brad Hein


2 Answers

You can use this:

public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    int[] systemAttrs = {android.R.attr.layout_height};
    TypedArray a = context.obtainStyledAttributes(attrs, systemAttrs);
    int height = a.getDimensionPixelSize(0, ViewGroup.LayoutParams.WRAP_CONTENT);
    a.recycle();
}
like image 131
Mohammad Ersan Avatar answered Sep 21 '22 22:09

Mohammad Ersan


that work :)... you need to change "android" for "http://schemas.android.com/apk/res/android"

public CustomView(final Context context, AttributeSet attrs) {
    super(context, attrs);
    String height = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "layout_height");
    //further logic of your choice..
}
like image 24
douarbou Avatar answered Sep 19 '22 22:09

douarbou