0

I'm trying to get the width and height of an imageView after the screen is rotated but it always returns 0 Note: onWindowFocusChanged doesn't get called after rotation because i handle the rotation manually. Also when i tried this code

ViewTreeObserver vto = mImgView.getViewTreeObserver();
    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        public boolean onPreDraw() {
            mImgView.getViewTreeObserver().removeOnPreDrawListener(this);              
            viewWidth = mImgView.getWidth();
            Log.e("Width =",viewWidth +"");
            return true;
        }
    });

it get called twice and returns different values one of them is correct !! My Image view display code is as follows

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    Configuration config=getResources().getConfiguration();


    if(config.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        setContentView(R.layout.quran_display_act);
        mImgtView = (ImageView) findViewById(R.id.rightImg_view);
        mImgtView.setImageBitmap(bitmap);
        //TODO hereee
       float viewWidth = mImgtView.getWidth();
        ...
    }
RamDroid
  • 123
  • 2
  • 7
  • It's not wrong for your view to end up being measured to multiple sizes. Layouts such as RelativeLayout may end up measuring views multiple times before determining their proper size. – Kevin Coppock Aug 08 '14 at 17:46
  • so how can i know the proper size ? – RamDroid Aug 08 '14 at 18:12
  • What do you actually need the size for? That might help to find a proper answer. – Kevin Coppock Aug 08 '14 at 18:12
  • I need to draw some rect/figure on a certain position over this imageview and this position is can be calculated relatively to the actual size – RamDroid Aug 08 '14 at 18:17
  • You could use an `OnGlobalLayoutListener` and just not detach the listener. As long as the code within your layout listener doesn't affect the view's layout, it won't cause an infinite loop. – Kevin Coppock Aug 08 '14 at 18:22
  • what's wrong with this vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { // TODO Auto-generated method stub viewWidthLand = mImgRightView.getWidth(); viewHeightLand = mImgRightView.getHeight(); //displayCurrentImage(); Log.e("onGlobalLayout",viewWidthLand+""); // displayCurrentImage(); } }); – RamDroid Aug 08 '14 at 18:26
  • You tell me. Does it work? What does `displayCurrentImage()` do? – Kevin Coppock Aug 08 '14 at 18:33
  • onGlobalLayout get called many times and the last one is the correct, so how can i get the last value as a work around – RamDroid Aug 08 '14 at 18:36
  • You could just update the position of your rect every time the layout changes, unless it's highly expensive, that's what I would suggest. – Kevin Coppock Aug 08 '14 at 18:37

2 Answers2

0

What's that, why call setContentView again? If you want to handle orientation change manually, remove the configChanges="orientation" from the manifest if it's there. Then every time the screen is rotated the whole activity is rebuilt. So you just have put that if-else-statement you posted inside onCreate() and leave onConfigurationChanged alone. This article should help.

Edit:

There is no problem with switching layouts on orientation change. The above is just one of the official ways. The important thing to note is: setContentView may not be called more than once! See setContentView() is not enough to switch between layouts? or Calling setContentView() multiple times. Rule of thumb: Always call setContentView inside onCreate right below super.onCreate and then never again.

So if you want to switch your layout at runtime when the orientation is changed, you have to remove "setContentView(R.layout.quran_display_act);" and use something else. The most notable options are:

  1. ViewFlipper, in which case your default layout is the first child of the ViewFlipper and you have to move everything inside quran_display_act.xml to your main layout XML and make it the second child of the ViewFlipper. Then you call next() on the ViewFlipper instead of setContentView on orientation change.

  2. Showing and hiding layouts. Here you also have to move everything inside quran_display_act.xml to the main layout of your activity. Call setVisibility(View.HIDE) on the layout you want to switch and setVisibility(View.VISIBLE) on the layout you want to show.

  3. Using Fragments, which is by far the most flexible and efficient option.

Using options 1 and 2 you'll be able to easily measure the site of your ImageView by using the getWidth() and getHeight() as you already are. It's a bit difficult with option 3, but still possible.

Community
  • 1
  • 1
0101100101
  • 5,786
  • 6
  • 31
  • 55
  • No i don't want the activity to be restarted each rotation, i just want to change the layout – RamDroid Aug 08 '14 at 16:01
  • what's the problem with switching between the landscape layout and portrait layout while rotation? it's common and recommended by android official tutorial – RamDroid Aug 11 '14 at 01:08
  • @RamDroid my answer should now answer all important questions you have. – 0101100101 Aug 11 '14 at 07:08
0

Just try like this Create a custom image view by extending ImageView class

override the onMeasure() method

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

setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
int width = MeasureSpec.getSize(widthMeasureSpec);

int height = MeasureSpec.getSize(heightMeasureSpec);



    if(getMeasuredWidth()!=0 && getMeasuredHeight()!=0)
    {
              //write your code here
              //it gets called many times ,so write a condition also to stop executing the same several times.
    }

}
Uday Sravan K
  • 1,305
  • 12
  • 18