Search code examples
javaandroid-studioimageviewfirebase-storageandroid-linearlayout

Remove large gap between images in Linear Layout Android Studio


I have a program that is adding imageViews to a linear layout within a horizontal scrollview, but each image ends up with a very large gap between each one. This looks terrible, and I want to reduce the gap between them, just so that they are closer together. This is how big the gap between each image is right now

This is the size of the current gap

Here is the code where I am adding them into the layout. I have tried changing the scaletype of the imageviews but I couldn't find a way to fix it. Any help would be appreciated.

    public void addPicturesToView(){
        LinearLayout pictureLayout = findViewById(R.id.qrImageLinearLayout);
        docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                List<String> imageList = (List<String>) documentSnapshot.get("Images");
                for(String imageString : imageList){
                   StorageReference storRef = storage.getReferenceFromUrl(imageString);
                    Task<byte[]> task = storRef.getBytes(1000000).addOnSuccessListener(new OnSuccessListener<byte[]>() {
                        @Override
                        public void onSuccess(byte[] imageBytes) {
                            Bitmap imageBitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
                            ImageView qrImage = new ImageView(QRcodeViewerActivity.this);
                            qrImage.setImageBitmap(imageBitmap);
                            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                            qrImage.setLayoutParams(params);
                            pictureLayout.addView(qrImage);
                        }
                    });
                }
                return;
            }
        });
        return;
    }

Solution

  • I have solved this issue, by setting AdjustViewBounds to true and adding padding I was able to make it look proper.

    Bitmap imageBitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
                                ImageView qrImage = new ImageView(QRcodeViewerActivity.this);
                                qrImage.setImageBitmap(imageBitmap);
                                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                                qrImage.setLayoutParams(params);
                                qrImage.setAdjustViewBounds(true);
                                qrImage.setPadding(5,0,5, 0);
                                pictureLayout.addView(qrImage);
    

    enter image description here