3

In my custom View .xml , i have defined width and height as w = 600dp , h = 700dp. Now i know getMeasuredHeight() / getMeasuredWidth() give values of width and height after view is drawn and they may differ from what i've given in .xml file , is there a workaround to get getMeasuredHeight() and getMeasuredWidth() values before view is actually drawn on layout, without use of onMeasure() ?

And How to calculate changed dp sizes in different screens ? like my 600h*700w when run on emulator converts to 300*300 .

Zulqurnain Jutt
  • 298
  • 3
  • 20

2 Answers2

1

You can override onSizeChanged() to get height and width of the view when it is drawn.refer below:

  @Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    mWidth = w;
    mHeight = h;
    super.onSizeChanged(w, h, oldw, oldh);
    Log.d(TAG, "onSizeChanged: " + " width: " + w + " height: " + h + " oldw " + oldw + " oldh " + oldh);
}

To convert dp into pixels you can use following code:

   sizeInPixels = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            sizeInDp, getResources().getDisplayMetrics());
rupesh jain
  • 3,410
  • 1
  • 14
  • 22
  • Problem is i can't get width and height of view outside of custom view class , and if i stored these values in static variable of custom view class still , it is not guaranteed , that i will get actual size , because i don't know when onSizechanged is actually gonna call . – Zulqurnain Jutt Jul 21 '15 at 20:47
  • onSizeChanged is called on layout..I used it in all my custom views – rupesh jain Jul 21 '15 at 21:01
  • In my particular case , as mentioned in my question , i am trying to get width and height of custom view in main activity.and on size changed is not called. – Zulqurnain Jutt Jul 21 '15 at 21:14
  • you can expose an API(public method) from the custom view,whihc can be used by the activity to get height width...soemthing like customeView.getHeight() and customView.getWidth – rupesh jain Jul 21 '15 at 21:18
  • i have done all of that , what i am getting is 0 always – Zulqurnain Jutt Jul 21 '15 at 21:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83913/discussion-between-rupesh-jain-and-bad-computer). – rupesh jain Jul 21 '15 at 22:03
0

I had kind of the same problem.

Here is my solution, how I came across this similar problem. There you can get the Width and Height before the Image is drawn with OnPreDrawListener()

     ImageView iv= binding.photoRoundProfile;
     ViewTreeObserver vto = iv.getViewTreeObserver();
     vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
   
           private int mFinalWidth = 0;
           private int mFinalHeight = 0;
   
           public boolean onPreDraw() {
   
               if(mFinalHeight != 0 || mFinalWidth != 0)
                   return true;
   
               mFinalHeight = iv.getHeight();
               mFinalWidth = iv.getWidth();
               Log.d("hilength","Height: " + mFinalHeight + " Width: " + mFinalWidth);
   
               ImageUtil.setPic(binding.photoRoundProfile, mCurrentPhotoPath);
               return true;
           }
       });
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
BenG
  • 28
  • 7