Android: Create grid with dynamic size in gridlayout or gridview

Ravi Vaghela picture Ravi Vaghela · Oct 24, 2015 · Viewed 9k times · Source

I am planning to make grid base application in which number of grids will change according to user's click. grid pattern:- 2*2 , 4*4 ... [like, user will click on correct grid then number of grid will increase]. I cn able to make grid but it is not fit in layout. so, how can i change the number of grid on user's click and how can i set size of grid dynamically.

Here i have attached the image which i want to achieve .

When user click on different color grid, Number of grid will increase like second image.

Image : 1

enter image description here

Image : 2

Number of grid increased on user's click.

enter image description here

Answer

Ravi Vaghela picture Ravi Vaghela · Nov 9, 2015

Finally i got answer of My question:

Here need to take SquareImageView for imageview and programatically set column of gridview, now see below code:

SquareImageView.java

public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
    super(context);
}

public SquareImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); // Snap to
                                                                    // width
}
}

Create Custome adapter for view inflator,

layout.xml

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

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity implements OnClickListener {

int counter = 1;

CustomGrid adapter;
static int colum = 2;

public static GridView gridview;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    adapter = new CustomGrid(MainActivity.this);
    gridview = (GridView) findViewById(R.id.gridview);

    gridview.setNumColumns(2);

    gridview.setAdapter(adapter);

}

public class CustomGrid extends BaseAdapter {
    private Context mContext;
    CustomGrid adgg;
    int mNumColumns = 2;
    private Random rand = new Random();
    GridView gg;
    int color = Color.argb(255, rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
    float alfa = 0.1f;
    int rand_ind = rand.nextInt((2 * 2) - 0) + 0;

    public CustomGrid(Context c) {
        mContext = c;
        // this.Imageid = Imageid;
        // this.web = web;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mNumColumns * mNumColumns;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        View gridView;

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View grid = new View(mContext);
        grid = inflater.inflate(R.layout.layout, null);
        final View imageView = (ImageView) grid.findViewById(R.id.image_grid);

        if (convertView == null) {
            if (position == rand_ind) {
                imageView.setBackgroundColor(getColorWithAlpha(color, alfa));
            } else {
                imageView.setBackgroundColor(color);
            }
        } else {
            grid = (View) convertView;
        }

        grid.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {


                if (position == rand_ind) {

                    // Correct postion click time if condition occur..........


                    if (counter == 2) {
                        mNumColumns = mNumColumns + 1;
                        alfa = alfa + 0.1f;
                    } else if (counter == 6) {
                        mNumColumns = mNumColumns + 1;
                        alfa = alfa + 0.08f;
                    } else if (counter == 8) {
                        mNumColumns = mNumColumns + 1;
                        alfa = alfa + 0.1f;
                    } else if (counter == 12) {
                        mNumColumns = mNumColumns + 1;
                        alfa = alfa + 0.1f;
                    } else if (counter == 14) {
                        mNumColumns = mNumColumns + 1;
                        alfa = alfa + 0.1f;
                    } else if (counter == 16) {
                        mNumColumns = mNumColumns + 1;
                        alfa = alfa + 0.1f;
                    } else if (counter == 18) {
                        mNumColumns = mNumColumns + 1;
                        alfa = alfa + 0.05f;
                    } else if (counter >= 18) {
                        // mNumColumns = mNumColumns + 1;
                        alfa = 0.8f;

                    }

                    gridview.setNumColumns(mNumColumns);
                    gridview.setAdapter(adapter);
                    adapter.notifyDataSetChanged();

                    rand_ind = rand.nextInt((mNumColumns * mNumColumns) - 0) + 0;

                } else {

                    // Here code for nagative click............ whem click
                    // nagative at time geanarate this code.........
                }
            }
        });

        return grid;
    }

    public int getColorWithAlpha(int color, float ratio) {
        int newColor = 0;
        int alpha = Math.round(Color.alpha(color) * ratio);
        int r = Color.red(color);
        int g = Color.green(color);
        int b = Color.blue(color);
        newColor = Color.argb(alpha, r, g, b);
        return newColor;
    }
}

@Override
public void onClick(View v) {
}
}

I hope this answer help full to any one.