0

I'm just not able to set LinearLayout parameters programatically. Everything I try, I get this exact exception.

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) outerLayout.getLayoutParams();
params.bottomMargin = 50;
outerLayout.setLayoutParams(params);


java.lang.ClassCastException: android.widget.AbsListView$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams

I have no idea why, but it seems I just can't get LinearLayout.LayoutParams on a view. outerLayout is my LinearLayout I am instantiating it with findViewById(). I've tried importing different things but no luck.

Here is my xml that represents one item in a list view. outer_item_layout is the layout i am calling getLayoutParams() on.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/outer_item_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/modeName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_weight="3"
            android:text="Medium Text"
            android:textSize="18dp" />

        <ImageView
            android:id="@+id/dropdown_speed"
            android:layout_width="23dp"
            android:layout_height="23dp"
            android:layout_gravity="center"
            android:layout_marginRight="20dp"
            android:background="?selectableItemBackgroundBorderless"
            android:clickable="true"
            android:src="@drawable/ic_dropdown"
            tools:ignore="InvalidId" />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:id="@+id/expandable_seekbar"
    android:layout_width="match_parent"
    android:layout_marginBottom="-50dip"
    android:visibility="gone"
    android:layout_height="50dip"
    android:orientation="horizontal">

    <SeekBar
        android:id="@+id/custom_mode_speed_seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp" />

</LinearLayout>

orglce
  • 513
  • 2
  • 7
  • 19

1 Answers1

0

The return type of a view's getLayoutParams() call is not dependent on the type of the view, but rather on the type of that view's parent. So, even though outerLayout is a LinearLayout, its layout params could be anything.

It seems that your outerLayout view is being hosted inside some sort of ListView, which is why the result of outerLayout.getLayoutParams() is of type AbsListView.LayoutParams.

Ben P.
  • 52,661
  • 6
  • 95
  • 123
  • Yes, it is hosted inside a ListView. I am trying to get these LinearLayout parameters inside a list view adapter. Is there any way I could get the parameters of a layout instead of it's parent (ListView)? – orglce Dec 05 '17 at 18:56
  • 1
    @orglce The purpose of `LayoutParams` is to provide information to the parent about how to lay out the child, so there's not really any concept of "the parameters of a layout instead of its parent"... only the parent cares about laying out the child. You could perhaps solve this specific problem by setting the bottom _padding_ of the LinearLayout, rather than the bottom _margin_. – Ben P. Dec 05 '17 at 19:26