Search code examples
androidimagepositionandroid-relativelayouthorizontalscrollview

Problems with the position of pictures in a horizontal scroll view


Well i have the following code, that loads 5 pictures and 5 chekboxes, one for each picture:

hLayout.postDelayed(new Runnable() {

                        DisplayMetrics metrics = getActivity().getResources().getDisplayMetrics();
                        int width = metrics.widthPixels;
                        int height = metrics.heightPixels;

                        @Override
                        public void run() {

                            if (coupons != null) {
                                int coupSize = coupons.size();
                                final int itemWidth = (width / 2);
                                final int itemHeight = (height / 2);
                                hLayout.removeAllViews();
                                for (int i = 0; i < coupSize ; i++) {
                                    Coupon coupon = coupons.get(i);
                                    if (coupon.getImage() != null) {
                                        RelativeLayout parent = new RelativeLayout(getActivity());
                                        RelativeLayout.LayoutParams linearparams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                                        parent.setLayoutParams(linearparams);
                                        final CheckBox cb = new CheckBox(getActivity());
                                        final ImageView iv = new ImageView(getActivity());
                                        //iv.setScaleType(ScaleType.CENTER_INSIDE);
                                        LayoutParams checkBoxParams = new LayoutParams();
                                        cb.setId(i);
                                        checkBoxParams.height = LayoutParams.WRAP_CONTENT;
                                        checkBoxParams.width = LayoutParams.WRAP_CONTENT;
                                        cb.setOnCheckedChangeListener(changeListener);
                                        RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(itemWidth, itemHeight);
                                        imageParams.height = itemHeight;
                                        imageParams.width = itemWidth;
                                        imageParams.addRule(RelativeLayout.BELOW, cb.getId());
                                        parent.addView(cb, checkBoxParams);
                                        parent.addView(iv, imageParams);
                                        hLayout.addView(parent);
                                        ImageUtil.loadImage(coupon.getImage(), iv, itemWidth, itemHeight, "", false, getActivity());
                                    }
                                }
                            }

                        }
                    }, 200);

The first checkbox and image are at their place. The problem is at the other images that are a bit lower than the first picture. Except the first picture, all of them are aligned, but lower that they should be (they should be aligned with the first picture). What have i done wrong?


Solution

  • Resolved with: if (cb.getId() == 0) { imageParams.topMargin = 72; }