Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crop and display a child while allowing child to take the full height of its content by setting height as wrap_content?

My child is a LinearLayout having a dynamic height, it can change during runtime, could includes images and consists of varying number of child views(which too might have children).

This child LinearLayout is inside a CardView. Now I want this card view to be collapsible. In other words when collapsed it needs to have a smaller height (say 150dp). In this mode the card should display only the top 150dp(minus padding) of the child LinearLayout's content. But the LinearLayout should be allowed to take the maximum size(though the bottom below the CardView's boundary is not shown, cropped) by setting its height as wrap_content. This is similar to what would happen if I had placed a ScrollView(when scrolled to the top) instead of a CardView. But when I do this with CardView. The LinearLayout does not take the maximum height to wrap its children's height instead it takes the height bounded by its Parent CardView.

This causes images to be scaled smaller and could result in many unexpected errors.

Is there a way to achieve the functionality? Is there a different kind of Layout other than cardview that directly support croping and displaying only part of its content while making the child feel like it can heighten indefinitely.

like image 441
SadeepDarshana Avatar asked Jan 30 '18 06:01

SadeepDarshana


1 Answers

Extend LinearLayout you are using in CardView with other class (e.g. MyFrameLayout) and override method:

@Override
protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {
    super.measureChildWithMargins(child, parentWidthMeasureSpec, widthUsed, View.MeasureSpec.UNSPECIFIED, heightUsed);
}

Then use it in layout, and during runtime you can change height param.

like image 56
RadekJ Avatar answered Sep 27 '22 17:09

RadekJ