Q:

Programmatically added Views not behaving

I've created many custom views and I am trying to add them to my fragment. They get added but I can't seem to get them to go where I want. There should be 2 columns and 3 rows but it ends up as 1 column with all of the custom views stacked on top of one another. Here is my code to add the views and set the layout params to the fragment layout:
RelativeLayout fm = (RelativeLayout) view.findViewById(R.id.fragmentLayout);

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
    CustomImages cs = new CustomImages(getActivity());
    cs.setId(R.id.one);
    cs.setLayoutParams(params);

    params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
    params.addRule(RelativeLayout.RIGHT_OF, cs.getId());
    CustomImages2 cs2 = new CustomImages2(getActivity());
    cs2.setId(R.id.two);
    cs2.setLayoutParams(params);

    RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params2.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
    params2.addRule(RelativeLayout.BELOW, cs2.getId());
    CustomImages3 cs3 = new CustomImages3(getActivity());
    cs3.setId(R.id.three);
    cs3.setLayoutParams(params);

    RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
    params3.addRule(RelativeLayout.RIGHT_OF, cs3.getId());
    CustomImages4 cs4 = new CustomImages4(getActivity());
    cs4.setId(R.id.four);
    cs4.setLayoutParams(params);

    RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params4.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
    params4.addRule(RelativeLayout.BELOW, cs4.getId());
    CustomImages5 cs5 = new CustomImages5(getActivity());
    cs5.setId(R.id.five);
    cs5.setLayoutParams(params);

A:

cs3.setLayoutParams(params);
cs4.setLayoutParams(params);
cs5.setLayoutParams(params);

I believe params there should be replaced with params2, params3 and params4 respectively.
UPDATE:
Also, you should specify LAYOUT_BELOW for all views which are not on top, and do it correctly:
params2.addRule(RelativeLayout.BELOW, cs.getId()); // not cs2
params3.addRule(RelativeLayout.BELOW, cs2.getId()); // add this
params4.addRule(RelativeLayout.BELOW, cs3.getId()); // not cs4

