Search code examples
androidandroid-layoutmeasure

View.getWidth() not work in onCreate method?


Why lColorWheel.getWith() method returns 0? I guess that it have something to do with onMeasure event, but i really cant understand from documentation how it works. Where I have to set dimensions for mDrawable?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_light);

    ShapeDrawable mDrawable;
    ivColorWheel=(ImageView)findViewById(R.id.ivColorWheel);
    lColorWheel=findViewById(R.id.lColorWheel);
    ld=(LayerDrawable)getResources().getDrawable(R.drawable.colorwheel);  

    mDrawable = new ShapeDrawable(new OvalShape());

    mDrawable.setIntrinsicWidth(lColorWheel.getWidth());
    mDrawable.setIntrinsicHeight(lColorWheel.getHeight());
    Log.d("lColorWheel.getWidth()",Integer.toString(lColorWheel.getWidth())); //returns 0, why?
    Log.d("lColorWheel.getHeight()",Integer.toString(lColorWheel.getHeight())); //returns 0, why ?

and appropiate XML file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:gravity="center_horizontal"
android:addStatesFromChildren="true" >

<RelativeLayout
    android:id="@+id/lColorWheel"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="20dp" 
    android:layout_centerHorizontal="true" 
    android:adjustViewBounds="true">

     <ImageView
        android:id="@+id/ivColorWheel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:adjustViewBounds="true"
        android:paddingBottom="0dp"
        android:paddingTop="0dp"
        android:src="@drawable/iconwheel"
        />

      <ImageView
          android:id="@+id/ivCenterButton1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerHorizontal="true"
          android:layout_centerVertical="true"
          android:maxHeight="5dp"
          android:maxWidth="5dp"
          android:onClick="clc"
          android:paddingBottom="0dp"
          android:paddingLeft="0dp"
          android:paddingRight="0dp"
          android:paddingTop="0dp"
          android:src="@drawable/center3" />

</RelativeLayout>

<SeekBar
     android:id="@+id/sbColorIntensity"
     android:layout_width="match_parent"
     android:layout_height="50dp"
     android:layout_alignParentLeft="true"
      android:layout_below="@id/lColorWheel"
     android:layout_marginLeft="20dp"
     android:layout_marginRight="20dp"
     android:layout_marginTop="20dp"
     android:background="@drawable/colorintensitystrip"
     android:max="255"
     android:maxHeight="-25dp"
     android:thumb="@drawable/thumb"
     android:thumbOffset="0dp"
 />



<SeekBar
    android:id="@+id/sbOrientation"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@id/sbColorIntensity"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="20dp"
    android:background="@drawable/orientationstrip"
    android:max="255"
    android:maxHeight="-25dp"
    android:thumb="@drawable/thumb"
    android:thumbOffset="0dp"
     />

<SeekBar
    android:id="@+id/sbWhiteIntensity"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@id/sbOrientation"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="20dp"
    android:background="@drawable/whiteintensitystrip"
    android:max="255"
    android:maxHeight="-25dp"
    android:thumb="@drawable/thumb"
    android:thumbOffset="0dp"
     />
 </RelativeLayout>

thx


Solution

  • You need to measure view's height and width if you want to use them before rendering finishes.

    Following code will help you to measure height and width of any view.

    imgView.measure(
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    imgView.layout(0, 0,
        imgView.getMeasuredWidth(),
        imgView.getMeasuredHeight());
    

    Write this code before use of height and width of view.

    Sometimes this approach will not give proper result. There is alternative of it. Below code will calculate height and width before draw on screen.

    ViewTreeObserver viewTree = imgView.getViewTreeObserver();
    viewTree.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        public boolean onPreDraw() {
            finalHeight = imgView.getMeasuredHeight();
            finalWidth = imgView.getMeasuredWidth();
            //print or do some code
            return true;
        }
    });