0

I would like to create a dynamic gridview programmaticaly. All things look good. But the margin for each cell is not displayed. Please help.

PagerAdapter:

@Override
public Object instantiateItem(ViewGroup container, int position) {
    Calendar viewData = getData(position);
    GridView gridView = new GridView(mContext);
    gridView.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
    gridAdapter = new GridAdapter(mContext, viewData);
    gridView.setNumColumns(4);
    gridView.setAdapter(gridAdapter);
    container.addView(view);
    return gridView;
}

GridAdapter

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.cal_single_cell, parent, false);
    layout.setLayoutParams(new GridView.LayoutParams(2, 2));
    TextView lblDisplay = (TextView) layout.findViewById(R.id.lbl_cell_name);
    lblDisplay.setText(getDisplayText(position));

    if(CalendarConverter.dateCompare(getData(position), Calendar.getInstance(), VIEW_LEVEL) == 0){
        layout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.backgorund_current_cell));
    }
    return layout;
}

And my cal_single_cell.xml

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

    <TextView
        android:id="@+id/lbl_cell_name"
        android:text="Jan"
        android:textSize="18dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="64dp"
        android:gravity="center" />

</LinearLayout>

view display result on google drive

  • Hi @NguyenDang, i answered on your problem, but no response, no information that my answer helped you or not. If my answer help then please set it is right answer. Thanks. – Maciej Sikora Jul 31 '16 at 19:46
  • Hi Maciej Sikora, It still not work for me, when I say: ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.cal_single_cell, parent, false); layout.setLayoutParams(new GridView.LayoutParams(2, 2)); I got a white page. And I see ViewHolder is a really good for performance. Thank you. – Nguyen Dang Aug 01 '16 at 17:22
  • Paste Your current code, i will help You, I see in comment that You are doing it all the time wrong. – Maciej Sikora Aug 01 '16 at 18:04

1 Answers1

0

I think Your main mistake is that You want to change cell margin and instead of this change GridView margin.

Use GridView attributes android:horizontalSpacing, android:verticalSpacing. More info in this question - Increase the grid spacing in android

Or change margin,padding of view inside row view ( Every cell view ). Programically in Adapters getView method do something like that:

view.setLayoutParams(/* your layout params */); //where view is cell view

In You code:

@Override
   public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = LayoutInflater.from(mContext);
    ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.cal_single_cell, parent, false);

    //****************************
    //here set padding or margin

    layout.setLayoutParams(/* your layout params */);

    //***************************
    TextView lblDisplay = (TextView) layout.findViewById(R.id.lbl_cell_name);
    lblDisplay.setText(getDisplayText(position));

    if(CalendarConverter.dateCompare(getData(position), Calendar.getInstance(), VIEW_LEVEL) == 0){
        layout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.backgorund_current_cell));
    }
    return layout;
}

IMPORTANT

Changing layout parameters in every getView is very not optimal, if You must do it then create ViewHolder.

Community
  • 1
  • 1
Maciej Sikora
  • 19,374
  • 4
  • 49
  • 50
  • Hi @Maciej Sikora, Thank for your support I just add GridView.LayoutParams to each cell in my GridView in my updated post. And I hope that enough information for you. Thank you very much. – Nguyen Dang Aug 03 '16 at 05:00