Shashank Degloorkar
Shashank Degloorkar

Reputation: 3221

ListView row set Left Margin programmatically in CustomAdapter

I have a ListView with its Custom Adapter.

in adapter's getview i want to set margin left of 15 dip to some rows. based on certain conditions.

  -----------------------------
 |  Row 1 (margin 15dip)       |
  -----------------------------

  -----------------------------
 |  Row 2 (margin 15dip)       |
  -----------------------------

       -----------------------------
      |  Row 2.1 (margin 30dip)     |
       -----------------------------

             -----------------------------
            |  Row 2.1.1 (margin 45dip)   |
             -----------------------------

  -----------------------------
 |  Row 3 (margin 15dip)       |
  -----------------------------

But when i set margin using the following code it gives ClassCastException

01-17 18:19:35.467: E/AndroidRuntime(14165): java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

Row layout

<?xml version="1.0" encoding="utf-8"?>
 <FrameLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/parentframeLayout"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginLeft="10dip"
 android:layout_marginRight="10dip" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="40dip"
    android:background="@color/alphaBlueMariner"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:paddingLeft="10dip" >

    <ImageView
        android:id="@+id/offlineImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:src="@drawable/ic_object_offline" />

    <ImageView
        android:id="@+id/parentImageView"
        android:layout_width="@dimen/small_indicator_icon_square"
        android:layout_height="@dimen/small_indicator_icon_square"
        android:layout_marginLeft="5dip"
        android:src="@drawable/ic_arrow_right" />

    <TextView
        android:id="@+id/tvParentName"
        style="@style/TextViewMedStyleShadowBlack"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="20dip" />
</LinearLayout>

</FrameLayout>

sample in my getView method

   Log.i("LayoutParams","1");
            // setting left margin
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    FrameLayout.LayoutParams.MATCH_PARENT,      
                    FrameLayout.LayoutParams.WRAP_CONTENT
                    );

            Resources r = context.getResources();

            int px = (int) TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_DIP,
                    15, 
                    r.getDisplayMetrics()
                    );

            if(requirementModel.getParentId() == 0)
            {

                params.setMargins(px, 0, 0, 0);

                parentFrameLayout.setLayoutParams(params);

            }
            else
            {

                params.setMargins(px+px, 0, 0, 0);

                parentFrameLayout.setLayoutParams(params);

            }
            parentFrameLayout.requestLayout();

}

return row;

this code runs but crashes when

return row;

Please help. thanks :)

Upvotes: 3

Views: 9182

Answers (3)

Akhil Dad
Akhil Dad

Reputation: 1814

You can cast the list view params to and do your job if you want to set margin in complete view but individual item margin can be set as suggested by others

ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) mListView
        .getLayoutParams();

marginLayoutParams.setMargins(leftMargin, topMargin, righttMargin, bottomMargin);

Upvotes: 0

Ethan Fang
Ethan Fang

Reputation: 1269

I suspect it is because LayoutParams is not LinearLayout type. Can you try this?

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,      
                LinearLayout.LayoutParams.WRAP_CONTENT
                );

Upvotes: 0

mik_os
mik_os

Reputation: 688

ListView expects AbsListView.LayoutParams that does not support margins. Try to set margins on the LinearLayout object instead of the FrameLayout. Or use paddings on the FrameLayout.

Upvotes: 10

Related Questions