getMeasuredWidth() not working - android

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

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

Android RecyclerView: getting width/height of View with "match_parent"

I have a layout with some Views that I need to change size depending on some parameters, that depend on width of the View. The width is set to "match_parent". So when I try to get the width it returns 0. Here is what I have tried so far:
mask.getWidth();
mask.getLayoutParams().width
ViewTreeObserver vto = mask.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
mask.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = mask.getMeasuredWidth();
}
});
So my question is how do I get the width in this situation?
Thank you.
After digging around I found a solution that answers my question but has a major problem that I am yet to resolve. We can use method post() that every View has and adds a Runnable to the message queue. So as I understand what happens is after View is fully measured in method post() we can get dimensions that we need like this:
holder.button.post(new Runnable() {
#Override
public void run() {
width = holder.button.getWidth();
}
});
However the problem here is that RecyclerView won't wait until this view is measured as and will go to next row and rows won't be updated according to measures that you expect.
If anyway has a way to go about this problem please leave a comment. Thank you.

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

Android - Need callback when View is shown

Is there a method equivalent to Dialog.setOnShowListener() for the View class in android ?
I need it because when I call getLocationOnScreen on a View (say foo) in the line immediately following someViewGroup.addView(foo), I get location = (0,0).
I cannot use any lifecycle method of the activity, because addition of foo is on run (press of a button). Any help would be highly appreciated. Thanks in advance.
You should use a ViewTreeObserver:
someViewGroup.addView(foo)
ViewTreeObserver vto = someViewGroup.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
///getLocationOnScreen here
ViewTreeObserver obs = someViewGroup.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
}
});

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