0

I have next view hiererchy:

     <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:gravity="center">
        <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/group_bg"
            android:gravity="center_vertical" 
            android:baselineAligned="false">
            <LinearLayout
                android:id="@+id/itemplace"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"
                android:layout_weight="1" >
                <include
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    layout="@layout/pin" />
            </LinearLayout>
            <FrameLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/arrow_black"/>
        </LinearLayout>
    </LinearLayout>

I need to get itemplace width before this hierarchy will be drawing. I can not use OnLayoutChangeListener because I use API level 7.

I know that I must use measure() and layout() methods to calculate size of my views but I dont know hot it to do.

Root view in my hierarchy must fill all width of screen, so itemplace width depended of parent size, parent paddings and FrameLayout width.

What must I do to get it width?

Nik
  • 7,114
  • 8
  • 51
  • 75

2 Answers2

2

I have find out way to get view width. I can get it after android measuret it but before drawing using OnPreDrawListener in ViewTreeObserver:

    view.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener()
    {
        @Override
        public boolean onPreDraw()
        {
            int avaliableWidth = view.getWidth() - view.getPaddingLeft() - view.getPaddingRight();

            Log.d("width", String.valueOf(avaliableWidth));

            return true;
        }
    });

From Android documentation:

Callback method to be invoked when the view tree is about to be drawn. At this point, all views in the tree have been measured and given a frame. Clients can use this to adjust their scroll bounds or even to request a new layout before drawing occurs.

Nik
  • 7,114
  • 8
  • 51
  • 75
  • but I also want to be able to calculate view width self using measure() and layout() methods. How it to do? – Nik Jul 11 '12 at 06:08
0

This is available since API 1:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
    int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
}
Carnal
  • 21,744
  • 6
  • 60
  • 75
  • I have not ability to change view class. I have only View object returned from LayoutInflater. – Nik Jul 11 '12 at 06:27
  • Alright so you have you View view = ... and you need the witdh of view? – Carnal Jul 11 '12 at 06:31
  • what method are you using when the width returns 0 ? – Carnal Jul 11 '12 at 06:31
  • The view hierarchy from top post I inflate from LayoutInflater and get View object. I know, that is view must fill all screen width. There is all that I have. I need to get width of one view from hierarchy before it hierarchy will be draw. That is all task. One way to do this I have find already - usage OnPreDrawListener. But it would be great if I known other way to get view width using measure() and layout() like it Android doing. – Nik Jul 11 '12 at 06:34
  • First I have tryed call measure(MeasureSpec.EXACTLY, MeasureSpec.EXACTLY); and then I call layout(0, 0, screenWidth, 100); Android documentation said, that using this two methods Android calculates views width and height before drawing. But after using this methods, view.getMeasuredWidth() returns 0. – Nik Jul 11 '12 at 06:38
  • Alright, nah I'm sorry i can't help. Thought at first your problem was something else. – Carnal Jul 11 '12 at 06:40