To make Dynamic view as you see it, I wrote the following code:
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:
android:layout_width="match_parent"
android:layout_height="200dp"
whole activity code:
public class MainActivity2 extends AppCompatActivity {
RelativeLayout relativeLayout;
List<UserView> mUserViewList;
List<Integer> mUidList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
relativeLayout = findViewById(R.id.relativeLayout);
mUserViewList = new ArrayList<>();
mUidList = new ArrayList<>();
int size = 5;
RelativeLayout.LayoutParams[] array = getgrid2(size);
for (int i = 0; i < size; i++) {
//createing user view and addding in view list
UserView userView = new UserView(this);
mUserViewList.add(userView);
//setting id and addint to id list
userView.setId(i);
mUidList.add(i);
//settting text to user view
userView.setTextView("User " + (i+1));
if (array[i]==null)
continue;
//setting layout
mUserViewList.get(i).setLayoutParams(array[i]);
//adding to views
if (i==4){
relativeLayout.addView(exptionalMethodFor5view());
}
relativeLayout.addView(mUserViewList.get(i));
}
}
private RelativeLayout.LayoutParams[] getgrid2(int size) {
// int width = Math.max(relativeLayout.getMeasuredWidth(),relativeLayout.getMeasuredHeight());
// int height = Math.min(relativeLayout.getMeasuredWidth(),relativeLayout.getMeasuredHeight());
int width = Resources.getSystem().getDisplayMetrics().widthPixels;
int height =dpToPx(200);
RelativeLayout.LayoutParams[] array =
new RelativeLayout.LayoutParams[size];
switch (size){
case 1: {
array[0] = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
array[0].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
break;
}
case 2:{
array[0] = new RelativeLayout.LayoutParams(
width/2,
RelativeLayout.LayoutParams.MATCH_PARENT );
array[0].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
array[0].addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE);
array[1] = new RelativeLayout.LayoutParams(
width/2,
ViewGroup.LayoutParams.MATCH_PARENT);
array[1].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
array[1].addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
break;
}
case 3: {
array[0] = new RelativeLayout.LayoutParams(
width/3,
RelativeLayout.LayoutParams.MATCH_PARENT );
array[0].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
array[0].addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE);
array[1] = new RelativeLayout.LayoutParams(
width/3,
ViewGroup.LayoutParams.MATCH_PARENT);
array[1].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
array[1].addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
array[2] = new RelativeLayout.LayoutParams(
width/3,
ViewGroup.LayoutParams.MATCH_PARENT);
array[2].addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
break;
}
case 4: {
array[0] = new RelativeLayout.LayoutParams(
width/2,
height/2 );
array[0].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
array[0].addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE);
array[1] = new RelativeLayout.LayoutParams(
width/2,
height/2);
array[1].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
array[1].addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
array[2] = new RelativeLayout.LayoutParams(
width/2,
height/2);
array[2].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
array[3] = new RelativeLayout.LayoutParams(
width/2,
height/2);
array[3].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
array[3].addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
break;
}
case 5: {
// array[0] = new RelativeLayout.LayoutParams(
// width/3,
// height/2 );
// array[0].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
// array[0].addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE);
// array[1] = new RelativeLayout.LayoutParams(
// width/3,
// height/2);
//top right
// array[1].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
// array[1].addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
array[2] = new RelativeLayout.LayoutParams(
width/3,
height/2);
array[2].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
array[3] = new RelativeLayout.LayoutParams(
width/3,
height/2);
array[3].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
array[3].addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
array[4] = new RelativeLayout.LayoutParams(
width/3,
height/2);
array[4].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
array[4].addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
break;
}
case 6: {
array[0] = new RelativeLayout.LayoutParams(
width/3,
height/2 );
array[0].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
array[0].addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE);
array[1] = new RelativeLayout.LayoutParams(
width/3,
height/2);
array[1].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
array[1].addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
array[2] = new RelativeLayout.LayoutParams(
width/3,
height/2);
array[2].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
array[3] = new RelativeLayout.LayoutParams(
width/3,
height/2);
array[3].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
array[3].addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
array[4] = new RelativeLayout.LayoutParams(
width/3,
height/2);
array[4].addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
array[5] = new RelativeLayout.LayoutParams(
width/3,
height/2);
array[5].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
array[5].addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
break;
}
default:{
array[0] = new RelativeLayout.LayoutParams(
width/2,
ViewGroup.LayoutParams.MATCH_PARENT);
array[0].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
array[0].addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE);
array[1] = new RelativeLayout.LayoutParams(
width/2,
ViewGroup.LayoutParams.MATCH_PARENT);
array[1].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
array[1].addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
break;
}
}
return array;
}
private LinearLayout exptionalMethodFor5view(){
LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setGravity(Gravity.CENTER);
linearLayout.setLayoutParams(layoutParams);
UserView newUserview1 = new UserView(this);
newUserview1.setLayoutParams(new LinearLayout.LayoutParams(
(Resources.getSystem().getDisplayMetrics().widthPixels)/3,
dpToPx(200)/2));
UserView newUserview2 = new UserView(this);
newUserview2.setLayoutParams(new LinearLayout.LayoutParams(
(Resources.getSystem().getDisplayMetrics().widthPixels)/3,
dpToPx(200)/2));
linearLayout.addView(newUserview1);
linearLayout.addView(newUserview2);
return linearLayout;
}
public static int dpToPx(float dp) {
float density = Resources.getSystem().getDisplayMetrics().density;
return Math.round(dp * density);
}
}