Q:

How to set margin for index 0 of listView

I have a proper list view which has an adapter that extends BaseAdapter. 
Everything works fine, but I need a margin of 30 dp for index 0. How to achieve this?
I tried the below 2 code but it crashes.
View subView = createButtonView((MenuButton) getItem(position));
        if(position==0){
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)subView.getLayoutParams();
            layoutParams.setMargins(30, 0, 0, 0);
            subView.setLayoutParams(layoutParams);
        }
        return subView;

public View getSubView(int position){
        View subView = createButtonView((MenuButton) getItem(position));
        if(position==0){
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
            subView.setLayoutParams(layoutParams);
        }
        return subView;
    }

Also I cannot use a headerView because the child view might not always be a same view, its will be dynamic.

A:

simple alternative is use
SpaceView with 30dp width in xml( space view is very light view in android)
and adjust the visiblity for positions
if (position == 0)
            yourxmlSpaceView.setVisibility(View.VISIBLE);
        else
            yourxmlSpaceView.setVisibility(View.GONE);

for 30dp you can use this method to convert int to pixels
public int dpToPx(int dp) {
        DisplayMetrics displayMetrics = context.getResources()
                .getDisplayMetrics();
        int px = Math.round(dp
                * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
        return px;
    }

