Facing issue While making dynamic views using Relative layout

164 Views Asked by At

I'm using relative layout for building the views/grid dynamically...

ex: if there is 1 user then,

[grid with users 1]1

if there is 2 user then ,

[grid with users 2]2

if there is 3 user then,

[grid with user 3]3

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

1

There are 1 best solutions below

0
On

enter image description hereTo 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);
    }
}