1

I'm retrieving data from WebService and I use a custom adapter ArrayAdapter to populate my listView. For each row, I have to set a left or right margin depending webservice return. So my idea was using this following code :

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 0, 0, 0);  // left margin
holder.mainContainer.setLayoutParams(layoutParams);  // Here is the problem

mainContainer is a LinearLayout

In getView method I use the view hodler pattern

MessageHolder holder = null;

if (convertView == null) {
    convertView = LayoutInflater.from(MessagesActivity.this).inflate(R.layout.thread_activity_listview_item, null);

    holder = new MessageHolder();
    holder.mainContainer = (LinearLayout) convertView.findViewById(R.id.main_container);

    convertView.setTag(holder);
} else {
    holder = (MessageHolder) convertView.getTag();
}

Here is my xml for a row :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_left_corner_discussion_on"
android:orientation="vertical"
android:padding="10dp" >

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="5dp"
    android:gravity="center_vertical" />

<TextView
    android:id="@+id/content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="2dp"
    android:layout_marginTop="2dp"
    android:gravity="center_vertical"
    android:textColor="@android:color/black"
    android:textSize="16sp" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:layout_marginBottom="2dp"
    android:layout_marginTop="2dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/updated"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="12sp" />
</LinearLayout>

But with this code, I have the following error :

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

I precise, I can't use padding in my situation. Moreover if I use AbsListView.LayoutParams instead of LinearLayout.LayoutParams, I can't set margin anymore.

Thanks

mrroboaat
  • 5,602
  • 7
  • 37
  • 65

3 Answers3

1

I don't think it is possible to dynamically set margin for a ListView row view. As a hack what you can do is split your row layout in to two parts as below

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal">

<LinearLayout>

<--!Blank layout which acts as margin, set weight to 10-->
</LinearLayout>

<LinearLayout>
 <--!Move the code for your row here, set weight to 90-->
</LinearLayout>

</LinearLayout>

Provide ids for your child LinearLayouts so that you can access them in code. Since the view group you access is linear layout you can set the weight of them in code.

Supreethks
  • 597
  • 6
  • 16
  • You change the layout of the adapter to set margin on a row, this is possible. – Booger Mar 04 '14 at 14:18
  • @Booger Check this link http://stackoverflow.com/a/16278769/1186983 . `AbsListView.LayoutParams` doesnt include margin support. And I guess OP has tried that. – Supreethks Mar 04 '14 at 14:19
  • Well, I am suggesting that OP not set List params, and instead set this in the adapter. You don't need the workaround you are suggesting. I think it is a bad idea to add empty layouts, as spacers (creates extra view containers, which is non-performant). Peace. – Booger Mar 04 '14 at 15:05
1

try this

holder.mainContainer = (LinearLayout) convertView.findViewById(R.id.main_container);
    LayoutParams params = holer.mainContainer.getLayoutParams();
    params.setMargins(30, 0, 0, 0); 
    holder.mainContainer.setLayoutParams(layoutParams);

you need to get params from view and just add margin to it.

Imtiyaz Khalani
  • 2,037
  • 18
  • 32
0

First you should get :

ListView lstView = (ListView) view.findViewById(android.R.id.list);

Then :

RelativeLayout.LayoutParams lpimgHeader = new RelativeLayout.LayoutParams(lstView.getLayoutParams());

finally :

lpimgHeader.setMargins(0, 0, 0, 0); <== Insert size margins
lstView.setLayoutParams(lpimgHeader);

Notice : Custom RelativeLayout to LinearLayout .

A.A
  • 1,138
  • 1
  • 16
  • 29