Get Height and Width of a child layout in contraintlayout - android

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

Related

How do you set the height of a view programmatically?

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

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

ViewPager does not wrap height to its content

ViewPager does not wrap its height to its content. For my layout I need to set the height dynamically because it is nested in other layouts.
Therefore I created a ScrollView as content of the ViewPager and check its height in onCreate method of the Fragment, but the ScrollView has always the same size.
How can I wrap height of the ViewPager to fit to its content?
final ScrollView sv = (ScrollView) v.findViewById(R.id.sv);
ViewTreeObserver vto = sv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int height = sv.getMeasuredHeight();
//always same height independent of content height
//need to resize ViewPager
ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) mPager.getLayoutParams();
params.height = height;
mPager.setLayoutParams(params);
}
});
Tried a lot of things, finally got it.
-initially set the ViewPager to a big height to fit all possible content and not to make its height reduced
-had to nest 2 LinearLayout to get the correct height.
hth

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

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