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.