I'm using relative layout for building the views/grid dynamically...
ex: if there is 1 user then,
if there is 2 user then ,
if there is 3 user then,
if there are 4 user then , 2 above and 2 below
if there are 5 then, [grid with user 5]4
if there are 6 then. [grid with user 6]5
my code is ,
private RelativeLayout.LayoutParams[] getgrid(int size) {
int width = Math.max(getMeasuredWidth(),getMeasuredHeight());
int height = Math.min(getMeasuredWidth(),getMeasuredHeight());
RelativeLayout.LayoutParams[] array =
new RelativeLayout.LayoutParams[size];
for (int i = 0; i < size; i++) {
if (i == 0) {
array[0] = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
array[0].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
} else if (i == 1) {
array[1] = new RelativeLayout.LayoutParams(width / 2, height);
array[0].width = array[1].width;
array[1].addRule(RelativeLayout.RIGHT_OF, mUserViewList.get(mUidList.get(0)).getId());
array[1].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
// array[1].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
} else if (i == 2) {
array[i] = new RelativeLayout.LayoutParams(width / 3, height);
array[1].width = array[i].width;
array[0].width = array[i].width;
//array[i].addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
array[i].addRule(RelativeLayout.RIGHT_OF, mUserViewList.get(mUidList.get(1)).getId());
} else if (i == 3) {
array[i] = new RelativeLayout.LayoutParams(width / 2, height / 2);
array[0].width = array[i].width;
array[1].width = array[i].width;
array[i - 1].width = array[i].width;
array[0].height = array[i].height;
array[1].height = array[i].height;
array[i - 1].height = array[i].height;
array[i - 1].addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
array[i].addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
array[i - 1].addRule(RelativeLayout.BELOW, mUserViewList.get(mUidList.get(0)).getId());
array[i].addRule(RelativeLayout.BELOW, mUserViewList.get(mUidList.get(1)).getId());
array[i].addRule(RelativeLayout.RIGHT_OF, mUserViewList.get(mUidList.get(i - 1)).getId());
} else if (i == 4) {
array[i] = new RelativeLayout.LayoutParams(width / 3, height / 2);
array[0].width = array[i].width;
array[1].width = array[i].width;
array[2].width = array[i].width;
array[3].width = array[i].width;
array[0].setMargins(width / 6, 0, 0, 0);
array[i].addRule(RelativeLayout.RIGHT_OF, mUserViewList.get(mUidList.get(i - 1)).getId());
array[i].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
} else if (i == 5) {
array[i] = new RelativeLayout.LayoutParams(width / 3, height / 2);
array[0].setMargins(0, 0, 0, 0);
array[i].addRule(RelativeLayout.RIGHT_OF, mUserViewList.get(mUidList.get(1)).getId());
array[i].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
}
}
return array;
}
the issue is there is another view right besides the relative layout so basically its not getting equally divided and distorted as well (distorted means some grid is not covering the full height)
please refer this:-- [issue with 6]6
[issue with 3]7 and so on
please help me out thanks in advance
Note: There is an exception for the 5 view because it needs to be wrapped inside another view (in my case, the linear layout).
Currently, the width and height of the relative layout are fixed as follows:
whole activity code: