Android calculate height of child View When overflow - android

I have nested LinearLayouts where Parent height is 90 but the child view may have different heights according to the text set inside it and may overflow its parent. I Want to animate Parent to final Height of child but both methods of getHeight() on child and getMeasuredHeight() on parent, return 90 not 400.
parent.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
targetHeight = parent.getMeasuredHeight();
//returns 90
and also
View child=((ViewGroup) parent).getChildAt(0);
int targetHeight=child.getHeight();
//returns 90

Try this code. Hope it'll work.
ViewGroup.LayoutParams params = yourView.getLayoutParams();
int height = params.height;

LayoutParams paramsChild = child.getLayoutParams();
LayoutParams paramsParent = parent.getLayoutParams();
paramsParent.height = paramsChild.height;
parent.setLayoutParams(paramsParent);

Related

Set zero height to constraintLayout child programmatically

How can I set the height of a ConstraintLayout child to be 0dp?
When I use layoutParams and do the following, ConstraintLayout thinks 0dp = match_parent
val layoutParams = view.layoutParams.apply {
this.height = newHeight
}
view.layoutParams = layoutParams
So if newHeight is 0 here, it thinks it's match_parent
I would like to avoid checking for 0 and if true set it to a negative value

How to get the Height and Width of the View?

I need to get the height and width of the View before adding into layout or its is display. I have tried with using MeasuredHeight and MeasuredWidth of the view but its retursn as 0. Could you please help me out to resolve this?
Below code snippet I have tried out:
int height = Column.MeasuredHeight;
int width = Column.MeasuredWidth;
You need to use a Layout change event to not receive 0 as the height.
If you ask it too soon, the layout isn't drawn yet. Use the ViewTreeObserver of the layout that you want to measure.
LinearLayout linearLayout = FindViewById<LinearLayout>(Resource.Id.linearLayout);
ViewTreeObserver vto = linearLayout.ViewTreeObserver;
vto.GlobalLayout += (sender, args) => {
System.Console.WriteLine (linearLayout.Height);
};
Try view.getWidth() and view.getHeight()
View view= LayoutInflater.from(this).inflate(R.layout.activity_main, null, false);
setContentView(view);
int viewWidth = view.getWidth();
int viewHeight = view.getHeight();

Android Resize selected item in GridView

I have a grid view displaying a number of ImageView with different background colors. Is it possible to scale just the selected item without affecting the placement of the other items in the GridView? Something like below?
I have tried setting the layout params on the underlying view that is returned when an item is clicked, but that seems to be chopping off part of the resized item and it not centering the selected item.
int newDimension = Utility.dpToPx(100);
float measuredWidth = view.getMeasuredWidth();
float measuredHeight = view.getMeasuredHeight();
GridView.LayoutParams layoutParams = (GridView.LayoutParams) view.getLayoutParams();
view.setPivotX(newDimension / 2);
view.setPivotY(newDimension / 2);
layoutParams.width = newDimension;
layoutParams.height = newDimension;
view.setLayoutParams(layoutParams);
close try calling, gridView.getItemAtPosition(position) and it will return your view do your operation then

In custom layout in onMeasure(), should textview handle its layout_height?

I have a custom layout that includes some TextView and other widgets. The TextViews have an XML attribute android:layout_height="".
The problem is that the layout_height is being ignored and, I think, treated as wrap_content.
I thought that the child views are supposed to handle their own layout_width & layout_height params. Do I need to do something in my custom layout's onMeasure() or is the problem elsewhere?
Thanks.
Apparently the layout_width and layout_height params need to be handled by the container, not the TextView. So, a little piece of code like this seems to do the trick (I'm just showing the height calculation in this example):
for (int i = 0, count = getChildCount(); i < count; i++)
{
View child = getChildAt(i);
ViewGroup.LayoutParams params = child.getLayoutParams();
int heightSpec;
if (params.height == ViewGroup.LayoutParams.MATCH_PARENT)
heightSpec = heightMeasureSpec;
else if (params.height == ViewGroup.LayoutParams.WRAP_CONTENT)
heightSpec = MeasureSpec.makeMeasureSpec (Integer.MAX_VALUE, MeasureSpec.UNSPECIFIED);
else
heightSpec = MeasureSpec.makeMeasureSpec (params.height, MeasureSpec.EXACTLY);
child.measure (widthSpec, heightSpec);
}

Android: Measuring child with sizes defined in its xml

I would like to know how to measure child view with width and height value which are defined in child's xml file.
I know that in onMeasure() method of my custom ViewGroup I should call:
child.measure(MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY));
But how to get childWidth and childHeight from xml code?
I know this is an old question. Essentially you want to use measureChild function provided by the ViewGroup. This function will take care of measuring it properly.
You can get them from LayoutParams of the child:
LayoutParams l = child.getLayoutParams();
childWidth = l.width;
childHeight = l.height;
ViewGroup's constructors are capable of reading the layout_height and layout_width parameters from xml.
Try this
RelativeLayout.LayoutParams params =
(android.widget.RelativeLayout.LayoutParams)child.getLayoutParams();
childWidth = params.width;
childHeight = params.height;
in the above code replace the RelativeLayout with the your parent layout

Categories

Resources