10

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?

Cœur
  • 32,421
  • 21
  • 173
  • 232
Seth Nelson
  • 2,568
  • 4
  • 19
  • 29

3 Answers3

26

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.

Romain Guy
  • 95,351
  • 17
  • 214
  • 199
  • 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. – satur9nine Feb 24 '11 at 06:57
  • 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 :) – Romain Guy Feb 24 '11 at 07:18
  • Personally I like the "use some sort of map" option better since it hides implementation details :) – satur9nine Feb 24 '11 at 21:25
  • 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. – Jo Jo Feb 25 '11 at 00:00
  • 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. – Romain Guy Feb 25 '11 at 01:47
  • 2
    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. – lilbyrdie Mar 17 '11 at 20:31
7

use it-

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

        webview.setLayoutParams(vc);

It will work

Ravi
  • 1,919
  • 3
  • 18
  • 35
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); 
user609239
  • 3,278
  • 5
  • 23
  • 26