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();
}
});
Related
I have a constraint layout in which there is a child layout (frame_layout). The width and height are set as 0dp with match_constraint. How can we get the height and width of this frame_layout once the layout is loaded on the screen ? Using layoutparams gives 0dp. getWidth or getHeight again gives 0dp.
final FrameLayout layout = (FrameLayout) findViewById(R.id.YOUR_VIEW_ID);
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener (new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int width = layout.getMeasuredWidth();
int height = layout.getMeasuredHeight();
}
});
I try to get a FrameLayout's height in onGlobalLayout(), this piece of code is in the onCreate() method:
final FrameLayout layout = (FrameLayout)findViewById(R.id.detail_frame);
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
layout.getViewTreeObserver().removeOnGlobalLayoutListener (this);
ViewGroup.LayoutParams lPF = layout.getLayoutParams();
int gg = layout.getHeight();
int hh = lPF.height;
}
});
The XML file:
<FrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/detail_frame">
...
...
</FrameLayout>
but when I was debugging, 'gg' got the right height value, 'hh' got -1. I'm doing this because I want to set the layout's height. How can I get the right value of LayoutParams?
Anyway to know if it's finished laying out?
You already did, onGlobalLayout is called when the view finishes laying out, that's why getHeight return non-zero pixel in height. If it's not drawn yet, getHeight will return 0
How can I get the right value of LayoutParams?
Nothing is wrong here, it's a right value. You get -1 from LayoutParams.height because it's MATCH_PARENT placeholder value, just like WRAP_CONTENT = -2. Android read this layout params value, then calculate the real size in pixel, which will be the value you get when calling getHeight.
I'm doing this because I want to set the layout's height
Just set the height value to the LayoutParams: layout.getLayoutParams().height = 100
I have 2 views - a container view, and a gauge view. I want to programmatically change the height of the gauge view. The problem is that when I get the height of the container view - it always comes back as 2.0 - even though the height is full-screen!
int fullHeight = containerView.getLayoutParams().height;
Number height = fullHeight * (thisTank.getTankTotalVolume().doubleValue()/thisTank.getTankMaxVolume().doubleValue());
Number width = gaugeView.getWidth();
//gaugeView.setLayoutParams(new LinearLayout.LayoutParams(width.intValue(), height.intValue()));
gaugeView.getLayoutParams().height = height.intValue() * fullHeight;
gaugeView.requestLayout();
Actually, if you set the view's height to WRAP_CONTENT or MATCH_PARENT in XML, the containerView.getLayoutParams().height should be equals to 2, its a default value...!
You have to two options,
First, you can defined a fix value in the XML file
Second, and recommended, you can use a tree observer for this purpose. Please see the below code...!
final View layout = (View)findViewById(R.id.YOUR_VIEW_ID);
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
this.layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = layout.getMeasuredWidth();
int height = layout.getMeasuredHeight();
}
});
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();
}
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);
}
});