1

So I have an ImageView that I have declared in my xml.

class.xml

<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:id="@+id/relativeLayout"
    tools:context=".ImageConfirmation" >

    <ImageView
        android:id="@+id/confirmationView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        />

</RelativeLayout>

And my Java file looks like.

MyClass.java

public class MyClass extends Activity{

    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_confirmation);
        imageView = (ImageView) findViewById(R.id.confirmationView);

        }
}

Now when I try to do imageView.getWidth() and imageView.getHeight() it always returns me ZERO. So I was thinking of using the protected void onSizeChanged (int w, int h, int oldw, int oldh) from here. But I am not sure if this is the right way to do it.

If yes then how should I go about implementing it in my code structure. My class already extends an Activity so not sure how to implement the onSizeChanged.

I also tried doing

ViewTreeObserver vto = confirmationView.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                imageViewSurfaceWidth = imageView.getWidth();
                imageViewSurfaceHeight = imageView.getHeight();
            }

        });

But this didn't work for me either. How to get the imageView width and Height.

zvisofer
  • 1,346
  • 18
  • 41
user2453055
  • 975
  • 1
  • 9
  • 19
  • Possible duplicate of http://stackoverflow.com/questions/19271609/imageview-getwidth-returns-0 – ckihm Feb 11 '14 at 09:27

2 Answers2

1

You can use onSizeChange method, for this way, you should write your custom ImageView and override onSizeChange. In this method the height and width will be provided.

But attentation, this method won't be called in the before onCreate() in Activity.

I believe you can use this way to get the height and width:

imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            imageViewSurfaceWidth = imageView.getWidth();
            imageViewSurfaceHeight = imageView.getHeight();
            Log.i("activity", "width = "+imageViewSurfaceWidth+", height = "+imageViewSurfaceHeight);
        }
    });

I use this way to for this purpose for a long time, and it works well.

You said this doesn't work, and I think it should not happen, maybe you should log the height and width you get in onGlobalLayout() to see what is wrong.

L. Swifter
  • 3,179
  • 28
  • 52
-1

I use this inside main activity:

@Override
public void onWindowFocusChanged(boolean hasFocus) {

 super.onWindowFocusChanged(hasFocus);
main_width=main_view.getWidth();
main_height=main_view.getHeight();


}

where main_view is your imageView.

alex
  • 12
  • 1
  • `onWindowFocusChanged` is not designed for this purpose. I think `addOnGlobalLayoutListener` is more suitable. – L. Swifter Feb 25 '17 at 03:17