how to chain onSizeChanged(),onMeasure(),onDraw() in custom view class - android

how to chain onSizeChanged(),onMeasure(),onDraw() in custom view class so I can get parent width amd parent height, so that I can scale my image to be fit exactly into the screen...kindly give any suitable example

Add this to your activity's onCreate
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
You should be able to get the width and height
over here.
layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});

Related

Android: How to get 3rd party view size

I wish to get size of 3rd party banner that I can't override to add onMeasure or onSizeChanged methods. Is there way to get banner size or at least height.
You could set a ViewTreeObserver:
view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
// Check the width and height here
}
});

Android: ImageView getting negative width

I'm not sure why, but when I set the width of an imageview (called c), it returns a negative value.
ivPiano is also an imageView.
ViewTreeObserver vto = llPiano.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
llPiano.getViewTreeObserver().removeGlobalOnLayoutListener(this);
c.setTop(ivPiano.getTop());
c.setLeft(ivPiano.getLeft());
ShowMessageLong(ivPiano.getWidth());
c.getLayoutParams().width = ivPiano.getWidth();
c.getLayoutParams().height= ivPiano.getHeight();
c.bringToFront();
ShowMessageLong(c.getWidth());
}
});
ShowMessageLong() just creates and shows a toast. The first time (ivPiano's width) shows 1200, then second one (c's width) returns -22. Any ideas why? Thanks in advance!!

How to get the width and heigh of an imageview

I have this XML code:
<LinearLayout
android:id="#+id/linearLayoutInner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#layout/gallery_image_background"
/>
Then this code:
LinearLayout linearLayoutInner = (LinearLayout) findViewById(R.id.linearLayoutInner);
ImageView imageView = new ImageView(thisActivityContext);
imageView.setImageResource(R.drawable.example);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(lp);
linearLayoutInner.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
linearLayoutInner.addView(imageView);
I then call an own function that is meant to scale the bitmap image until one of the sides reach the edge (i.e. so if the original bitmat is twice as wide as tall, it will keep the proportion inside the imageview, something that is apparently not supported by any scaletype setting):
SharedCode.sharedUtilScaleImage(imageView);
And here comes the problem. That function needs to know the size of the view containing the bitmap drawable. if imageView behaves correctly, it should use MATCH_PARENT and hence give the width/height of the linearLayoutInner. However, the following code returns zero:
int heightParent = max(imageView.getLayoutParams().height, imageView.getHeight());
int widthParent = max(imageView.getLayoutParams().width, imageView.getWidth());
How do I solve this? And why am I returned 0 instead of the correct height/width?
You're probably calling the code too early, before the View's onMeasure() has been called. Until that happens, its size is unknown.
final ImageView iv....
ViewTreeObserver vto = iv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
//Measure
iv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
final ImageView imageView = (ImageView )findViewById(R.id.image_test);
ViewTreeObserver vto = imageView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
imageView .getViewTreeObserver().removeGlobalOnLayoutListener(this);
imageView.getHeight(); // This will return actual height.
imageView.getWidth(); // This will return actual width.
}
});
Seem like you might be calling from onCreate(). u need to wait for activity window to attached and then call getWidth() andgetHeight() on imageView.You can try calling getWidth() and getHeight() from onWindowFocusChanged() method of your activity.
Edit
#Override
public void onWindowFocusChanged(boolean hasFocus){
int width=imageView.getWidth();
int height=imageView.getHeight();
}

Dimension of my Scaled ImageView

How can I determine the dimensions of my Scaled ImageView? I tried using getWidth() & getHeight it always returns 0. My xml layout for the ImageView is as follows:
<ImageView
android:id="#+id/imgView"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/my_image" />
Please suggest something.
You are probably calling getWidth() method too early. The width and height of your ImageView will not be available until the View has been rendered on the screen. Try getting them within onWindowFocusChanged:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
int width = iv.getWidth();
int height = iv.getHeight();
// ...
}
You won't get those dimensions in onCreate. The view isn't measured yet and the hierarchy is not set. Those will work but you need to use them somewhere after the app has drawn the screen or the alternative is to set a ViewTreeObserver to notify you when the layout is set.ViewTreeObserver
Here is how its done:
call getVieeTreeObserver() to get a reference to the viewTreeObserver in your activity.
then add a onGlobalLayoutListener:
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
ViewTreeObserver obs = MyActivity.this.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
}
});
Inside this onGlobalLayout method you can use your view and get its width and height and do whatever with them.

Android layoutParams getWidth() getHeight()

These methods return correct values when retrieved dynamically for eg. in onCreate() , but the only exception is when the dimension is specified in the xml layout file as undefined ,
like 'wrap content' etc. Is there a way to retrieve such values too ???
This should return the correct width and height. onCreate is called before the layout of the child views are done. So the width and height is not calculated yet. To get the height and width. Put this on the onCreate method
LinearLayout layout = (LinearLayout)findViewById(R.id.YOUD VIEW ID);
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
hs.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = layout.getMeasuredWidth();
int height = layout.getMeasuredHeight();
}
});

Categories

Resources