Search code examples
androidviewgestureonscrolllistener

Change height of view programmatically with a float value (Android)


So what I have is just a simple view that is defined in the xml and what I want to do is using the onScroll gesture, change that view's height. Only problem is LayoutParams expects integer values, whereas the change in distance provided by onScroll is a float value. How can I resize this height without losing the precision of the float?

Here is what I have in the onScroll method:

int newHeight = mTopView.getHeight() + (int)distanceY;
if (newHeight <= maxHeight) {
    mTopView.setLayoutParams(new LinearLayout.LayoutParams(mTopView.getWidth(), newHeight));
}

Solution

  • As Jaap van Hengstum mentioned: "The LayoutParams dimensions are in pixels which is the smallest possible unit on the screen; you can't lose precision"

    So just cast that float to an int (and maybe use int newHeight = (int) (height + 0.5f); suggested again by Jaap), and everything will be smooth and cool.