View.getWidth () not working in onCreate method?

Why does the lColorWheel.getWith () method return 0? I suppose this has something to do with the onMeasure event, but I really can't understand from the documentation how this works. Where do I need 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 approve the 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

+6
source share
4 answers

You need to measure the height and width of the view if you want to use them before rendering is complete.

The following code will help you measure the height and width of any kind.

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

Write this code down before using the height and width of the view.

Sometimes this approach will not give the desired result. There is an alternative to this. Below code will calculate the height and width before drawing on the 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; } }); 
+15
source

When your onCreate method onCreate executed, Android has not yet completed the walkthrough of the layout or measure the pass.

You can measure the size of your view using the measure method, but I would strongly recommend that you call the ViewTreeObserver callback instead , especially since you are using a RelativeLayout (which performs two layouts on the tree).

Using ViewTreeObserver.OnGlobalLayoutListener ( link ), you can make any adjustments to your layout as soon as you know exactly where each element will be placed and their corresponding sizes.

+1
source

You call getwidth () too soon, the user interface is not ready to get this value. You need an Observer. I think this value is set right after the first paint of the user interface. Try this in the onCreate () method:

  final ViewTreeObserver vto = myView.getViewTreeObserver(); if (vto.isAlive()) { vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { public void onGlobalLayout() { int witdth = lColorWheel.getWith() // remove the listener... or we'll be doing this a lot. ViewTreeObserver obs = promotionSub_ly.getViewTreeObserver(); obs.removeGlobalOnLayoutListener(this); } }); } 
0
source

As stated in CommonsWare here

You call getWidth () too early. The user interface has not been laid out on the screen yet.

I doubt that you want to do what you do one way or another - widgets being animated do not change their interactive areas, and therefore the button will still respond to clicks in the original orientation, regardless of how it rotates.

In this case, you can use the measurement resource to determine the size button, then specify this measurement resource from your layout file and your source code to avoid this problem.

0
source

Source: https://habr.com/ru/post/947018/


All Articles