Android: ImageView getting negative width - android

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!!

Related

getHeight() isn't returning correct value -using ViewTreeObserver- at first run

In MainActivity you click on a camera button, take a picture and then the image is shown on a PreviewActivity and then I should get the Width and Height of the imageView.
I tried to show a log message showing the height value when I touch the screen now the height value is corrected; so I think the problem is that the imageView isn't being laid out probably at first or something. "it got the height value before everything is laid right..", even though I'm using getViewTreeObserver().
ViewTreeObserver viewTreeObserver = mImageView.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
mImageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
setImage(mCurrentPhotoPath, selectedImageUri);
}
});
}
Edit: I figured out that the problem is that the height value is taller than it acually is; because when opening the Camera app, the statusBar is hidden and it gets shown back again on PreviewActivity, but the height of the imageView is measured before statusBar is back up again, which causes the problem and causes the Height to be taller.
you could try get the width in post method like:
mImageView.post(new Runnable(){
#Override
public void run() {
mHeight = mImageView.getHeight(); //height is ready
}
});
As I know, runnable task in post will be executed after view's measure and layout.
So, the Only way I was able to fix this problem is; if we got the image from the Camera (and if opened from MainActivity) then get the height and width after a little delay, so that everything is laid out probably.
//-- Get image path from previous activity
mCurrentPhotoPath = getIntent().getStringExtra("imagePath");
selectedImageUri = getIntent().getData();
helper.setImage(mCurrentPhotoPath, selectedImageUri); //set image to imageView
if (cameraOpenedFromMainActivity) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//method to get height and width and do stuff
}
}, 500); //increase until everything is fine
}else {
ViewTreeObserver viewTreeObserver = mImageView.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
mImageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//method to get height and width and do stuff
}
});
}
}
//--

getMeasuredWidth() not working

This code is not working well for my nexus 7.First time when i install(debugge)its give me value
"1286" but after close the app and again i start it then give me width "1542" .
i know this one correct code but it is not worked why it give wrong value???
ViewTreeObserver vto = horizontalLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
horizontalLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
Log.i("WIDTH","Width "+horizontalLayout.getMeasuredWidth()+" width "+horizontalLayout.getWidth());
}
});
any idea why it is happened???

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();
}

Drawing a bitmap with relative layout returns illegalargumentexception

I am trying to create a bitmap out of a relative layout that i have created programmatically.
The Realtivelayout is showing as expected but when i try to create a bitmap , it returns illegal argument exception that height and width must be > 0
this is how i am doing it
Bitmap.createBitmap(saveLayout.getWidth(),
saveLayout.getHeight(), Bitmap.Config.ARGB_8888);
Any pointers?
Edit: added this code, too:
ViewTreeObserver viewTreeObserver = saveLayout.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver
.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
saveLayout.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
viewWidth = saveLayout.getWidth();
viewHeight = saveLayout.getHeight();
}
});
}
In above given code onGlobalLayout() also never seemed to be called.
If you're programmatically creating the saveLayout, you may be asking for its width and height too early. (just an educated guess, because I can't see the rest of your code)
See this explanation
The reason why that createBitmap() call throws that exception is:
if the width or height are <= 0
So, try to defer creating the Bitmap until after the view appears, or find another way to calculate the width/height.
This question has a few options/answers for how to defer this calculation to avoid 0 results

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

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);
}
});

Categories

Resources