programmatically change layout height, ClassCastException?
Asked Answered
P

3

11

I'm trying to "animate" a WebView to drop down and reveal its contents. I've written a handler to increase the height by 1 each time, however, I'm running into a ClassCastException. The code I'm using is:

WebView.LayoutParams params = new WebView.LayoutParams(wv.getLayoutParams());
params.height = height;
wv.setLayoutParams(params);
height++;
this.sleep(20);

On the line wv.setLayoutParams(params), I get a:

java.lang.ClassCastException: android.widget.AbsoluteLayout$LayoutParams

How do I fix this?

Philander answered 24/2, 2011 at 4:53 Comment(0)
T
27

The layout paramaters should be of the type of the parent of your view. For instance, if your WebView is inside a LinearLayout, use LinearLayout.LayoutParams.

Tetrabranchiate answered 24/2, 2011 at 5:18 Comment(6)
Any clue as to why Android was programmed this way rather than just a single LayoutParams class for all ViewGroups? The current implementation seems like one of the less elegant aspects of Android.Valise
Because all layouts do not support the same features. Having a single LayoutParams class means we would either use some sort of a map or cram all the possible layout params features in it. It's not better :)Tetrabranchiate
Personally I like the "use some sort of map" option better since it hides implementation details :)Valise
Yeah a map would have been better. It would have prevented this problem from happening to OP. As an application developer, I'd much rather have a cleaner api than save a couple bytes of memory.Foreclosure
It's not about saving memory (the map would actually probably use less memory), it's about type safety. With the map you lose compile-time warnings for instance.Tetrabranchiate
And yet, the types can't be checked at compile time anyway, otherwise there would be a compile warning instead of a runtime classcastexception. Although I understand why it's the parent (because that's who is doing the layout), it's not at all obvious based on the documented parameter type of ViewGroup.LayoutParams.Forewarn
S
7

use it-

ViewGroup.LayoutParams vc=webview.getLayoutParams();
        vc.height=700;
        vc.width=450;

        webview.setLayoutParams(vc);

It will work

Sanguinaria answered 2/1, 2013 at 13:10 Comment(0)
K
1

following is the code of setting size of activity, i hope this will solve your problem. In my case this code works.

WindowManager.LayoutParams params = getWindow().getAttributes();    
       params.height = 200;
       params.width = 220;         

       this.getWindow().setAttributes(params); 
Keppel answered 24/2, 2011 at 5:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.