Rectangle 27 82

well, that was painful but I finally got it figured out. The most important thing to remember (that I just realized) is that of all the myriads of LayoutParams, you need to use the one that relates to the PARENT of the view you're working on, not the actual view.

So in my case, I was trying to get the TextView margins working, but it was being put inside a TableRow. The one simple change was ensuring that the type of LayoutParams being used were the ones for TableRow:

private void buildTextView(){   
    // the following change is what fixed it
    TableRow.LayoutParams paramsExample = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1.0f);
    txtviewExample.setId(textViewExampleID);
    txtviewExample.setBackgroundResource(R.drawable.textview_9patch_white);
    txtviewExample.setGravity(Gravity.CENTER);
    txtviewExample.setTextColor(getResources().getColor(android.R.color.black));
    paramsExample.setMargins(20, 20, 20, 20);
    txtviewExample.setPadding(20, 20, 20, 20);
    txtviewExample.setTextSize(40); 
    txtviewExample.setText("customExample");
    txtviewExample.setLayoutParams(paramsExample);
}

Thanks guys, hopefully this will come in handy for somebody else down the line, as I saw a lot of semi-related questions in the forums here, but not one that really defines the problem.

THANK YOU! I was have the same issue, it was because I was using LinearLayout.LayoutParams instead of TableRow.LayoutParams... dumb mistake but you saved me from a lot of frustration!

android: LayoutParams for TextView makes the view disappear, programma...

android textview layoutparams
Rectangle 27 79

Nice to know, you solved my issue, I was trying to add the constraint on the child instead of the parent. Now it's working like a charm.

Ohhh.. the PARENT. Makes sense. Was setting it based on its own type, which was a ViewGroup of a different kind (ie: LinearLayout where parent was FrameLayout). Subtle!

LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout...

android
Rectangle 27 28

A change to the layout params only take effect on the next layout pass.

So, to be safe, call requestLayout() always after touching the layout params. setLayoutParams() works, too, though it's not strictly necessary when modifying the params in-place.

Since LayoutParams are used to communicate from the child to the parent, shouldn't we need to call requestLayout on the parent once the child's LayoutParams change?

@vlazzle Layout requests on children propagate to the parent as well.

Oh yeah I see that here. Thanks @laalto!

android - After changing a property on a LayoutParams object, do I nee...

android layoutparams
Rectangle 27 40

dialogTitle.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

Here you're replacing existing layout params of correct type AbsListView.LayoutParams with more generic ViewGroup.LayoutParams. Layout params type is that of the parent container of the view.

If you need to modify existing layout params, access them with getLayoutParams(), modify it, and call requestLayout() to notify that the layout has changed.

fooView.setLayoutParams(new LayoutParams(123, 456));
LayoutParams lp = fooView.getLayoutParams();
lp.width = 123;
lp.height = 456;
fooView.requestLayout();

getLayoutParams() won't work if you are trying to set the size before you add the view to a layout, such as if you are extending the view. Then you have to discover that the underlying implementation is android.widget.AbsListView.LayoutParams and instantiate that: setLayoutParams(new android.widget.AbsListView.LayoutParams(w, h)). This completely breaks encapsulation (exposing the internal implementation of LayoutParams) and is an API bug.

"Layout params type is that of the parent container of the view." - this is the golden statement here. Whichever layout you are trying to set params has to have params as per his parent. (That's what I think the problem is).

I ran into the same class cast exception when returning a view in Adapter.getView with ViewGroup.LayoutParams. However I'd like to mention that the class cast exception seems to happen on older API level (I tested API 19). On newer API level (tested on 22), there is no exception at all. So the underlying implementation seems to be able to deal with returning a view with ViewGroup.LayoutParams.

android.view.ViewGroup$LayoutParams cannot be cast to android.widget.A...

android
Rectangle 27 17

Your image already has layout params. Instead of replacing them with a new object (which is of wrong type in this case, ViewPager expects them to be ViewPager.LayoutParams), you can modify existing layout params like this:

ViewGroup.LayoutParams params = img.getLayoutParams();
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
params.height = 100;
img.requestLayout();

Note that unconditionally requesting re-layout in onGlobalLayout() can be a bad idea, leading to infinite layout passes. I haven't verified it myself whether this is the case.

This is causing a loop. I tried to put my code in question just after onGlobalLayout() and its working fine but I noticed that my code is getting called before onGlobalLayout(). This appears strange just like threads do. So I am bound to put it inside onGlobalLayout().

Yes - could you clarify why you need to modify the layout in a global layout listener? There's probably a better way. (This answer addresses the class cast exception issue.)

http://stackoverflow.com/questions/18533224/getting-end-position-of-the-textview-and-imageview-with-respect-to-top-of-the-sc
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

android - ClassCastException ViewGroup$LayoutParams cannot be cast to ...

android classcastexception
Rectangle 27 22

android.widget.AbsListView.LayoutParams
android.widget.LinearLayout.LayoutParams

layout - ClassCastException: android.widget.LinearLayout$LayoutParams ...

android layout
Rectangle 27 9

Assign an ID to your LinearLayout in xml file:

<LinearLayout
        android:id="@+id/DrawerLinear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <!-- YOUR LISTVIEW IS HERE -->
</LinearLayout>

Then in you java class, Cast this LinearLayout:

private LinearLayout    DrawerLinear;
DrawerLinear = (LinearLayout) findViewById(R.id.DrawerLinear);

And in your onPrepareOptionMenu, put this:

public boolean onPrepareOptionsMenu(Menu menu) {

    boolean drawerOpen = mDrawerLayout.isDrawerOpen(DrawerLinear);
    menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}

kindly correct the little but difficult to notice error "findviewbyid" to "findViewById"

navigation drawer - LinearLayout$LayoutParams cannot be cast to androi...

android navigation-drawer
Rectangle 27 33

The problem is caused by wrapping the control you're returning from the adapter in a linearlayout. Lose the LinearLayout from your layout file (just put the TextView as the root) and I think it should work.

To expand slighly: you're creating your control, and then adding it into a LinearLayout. At this point a LinearLayout$LayoutParams object is allocated for it and set as its layout params. You then pull it out of the linear layout and put it into your list view, which doesn't understand the LinearLayout$LayoutParams.

Alternatively, return row from getView rather than (as I presume you are doing at the moment, as you have cut the return line out of your quoted code) returning tv.

(Another, unrelated hint: you are inflating your layout and then discarding it when convertView != null; this will result in poor scrolling performance on slow devices. Only perform the inflation if you're actually going to need it.)

yes! it worked.. but as you said, the scrolling is kinda jerky.. how do i smoothen it out without doing inflation? as in how do i get a custom layout for the list without inflating it? and i didnt understand the discarding part.. how is it getting discarded? the uri source of this list is from the mediastore..

Thank you for this @Jules , it helped understanding the issue! Another thing you can do if you still want to use the tv object is to set its layout params to be an instance of AbsListView.LayoutParams, ie: tv.setLayoutParams(new AbsListView.LayoutParams(width, height);

"Alternatively, return row from getView rather than (as I presume you are doing at the moment, as you have cut the return line out of your quoted code) returning tv." That worked like a charm.Thanks.

@TomSusel Thanks for the suggestion worked like a charm ! +1

This answer is misleading. The OP is probably returning tv and not row. If the OP changed the method to return the row then the class cast exception would not occur. There is no reason not wrap the row's TextView in a LinearLayout if that is what you intend.

Android - ClassCastException - on LayoutParams from LinearLayout to Ab...

android android-listview
Rectangle 27 10

WindowManager has an updateViewLayout() member... surely that's exactly what you want?

(as originally replied in comment)

This method is helpful but be careful because some properties like the Window type cannot be changed once set, you will get an FC if you try it.

setLayoutParams

Where can i use this updateViewLayout() method?

Android: change LayoutParams of View added by WindowManager - Stack Ov...

android view layoutparams
Rectangle 27 9

if (android.os.Build.VERSION.SDK_INT > 22) {
            container = (View) pwindow.getContentView().getParent().getParent();
        }else{
            container = (View) pwindow.getContentView().getParent();
        }

Android M ClassCastException: FrameLayout$LayoutParams cannot be cast ...

android android-6.0-marshmallow layoutparams android-popupwindow
Rectangle 27 9

if (android.os.Build.VERSION.SDK_INT > 22) {
            container = (View) pwindow.getContentView().getParent().getParent();
        }else{
            container = (View) pwindow.getContentView().getParent();
        }

Android M ClassCastException: FrameLayout$LayoutParams cannot be cast ...

android android-6.0-marshmallow layoutparams android-popupwindow
Rectangle 27 9

GridView.LayoutParams
Gallery.LayoutParams
imageView.setLayoutParams(new Gallery.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

It works but it's still very slow.

android - ClassCastException: AbsListView$LayoutParams cannot be cast ...

android android-asynctask android-gallery
Rectangle 27 1

private TextView txtviewExample = new TextView(this);

private void buildTextView(){   
    LayoutParams paramsExample = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1.0f);
    txtviewExample.setId(textViewExampleID);
    txtviewExample.setBackgroundResource(R.drawable.textview_9patch_white);
    txtviewExample.setGravity(Gravity.CENTER);
    txtviewExample.setTextColor(getResources().getColor(android.R.color.black));
    paramsExample.setMargins(20, 20, 20, 20);
    txtviewExample.setPadding(20, 20, 20, 20);
    txtviewExample.setTextSize(40); 
    txtviewExample.setText("customExample");

   setContentView(txtviewExample);//when you don't use SETTER method for TextView you can't view the desireable text on the UI//
}

use setContentView(txtviewExample) to make text visible on the UI screen

android: LayoutParams for TextView makes the view disappear, programma...

android textview layoutparams
Rectangle 27 20

you probably are returning the tv instead of row

public View getView(int position, View convertView, ViewGroup parent) {
    TextView tv;

    LayoutInflater inflater = getLayoutInflater();
    View row = View.inflate(mContext,R.layout.item,null);
    String id = null;
    if (convertView == null) {
        tv =  (TextView) row.findViewById(R.id.TextView01);;
    } else
        tv = (TextView) convertView;
    }
 return row;  // returning tv brings to ClassCast!

Android - ClassCastException - on LayoutParams from LinearLayout to Ab...

android android-listview
Rectangle 27 7

Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance .

support.v7
import android.support.v7.app.AlertDialog
import android.app.AlertDialog

But there is a reason for using the support version! I got exactly the same problem - my code worked and the problem came with a newer version of the support library!

@IntelliJ Amira That's why this is not a solution at all! The problem was introduced with v25 of the support library, it was not there in v24! And somebody using the support AlertDialog does this for a reason and not for fun. Thus, I cannot see how this is a valid solution... sorry!

ClassCastException: android.support.v7.widget.LinearLayoutCompat$Layou...

android android-layout android-alertdialog
Rectangle 27 5

Use the correct LayoutParams class, it should be

imageView.setLayoutParams(new Gallery.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

(The superclass of LayoutParams is Gallery instead of GridView)

It works but it's still very slow.

android - ClassCastException: AbsListView$LayoutParams cannot be cast ...

android android-asynctask android-gallery
Rectangle 27 5

So it turns out that on API < 17 FrameLayout can't be cast correctly to AbsListView params.

What I did was set AbsListView LayoutParams instead of FrameLayout params:

setLayoutParams(new AbsListView.LayoutParams(100,100));

java - android.widget.FrameLayout$LayoutParams cannot be cast to andro...

java android classcastexception framelayout android-runtime
Rectangle 27 4

View
ViewGroup
setLayoutParams
LayoutParams
LinearLayout
ViewPager#LayoutParams
LinearLayout#LayoutParams

ClassCastException: android.widget.LinearLayout$LayoutParams cannot be...

android android-layout android-viewpager
Rectangle 27 3

((ViewGroup)view.getParent()).removeView(view);

android - How to removeView(view, layoutparams)? - Stack Overflow

android view layoutparams
Rectangle 27 3

if (android.os.Build.VERSION.SDK_INT > 22) {
            container = (View) pwindow.getContentView().getParent().getParent();
        }else{
            container = (View) pwindow.getContentView().getParent();
        }

android - FrameLayout$LayoutParams cannot be cast to WindowManager$Lay...

android android-6.0-marshmallow android-popupwindow