how to add multiple imageview dynamically

895 Views Asked by At

im about to make like photo album so we can upload multiple photo (like facebook, etc)

ivAttachment1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(strPhoto1 == "" && PageType.equals("NewRequestMedical")) {
                Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
                photoTemp = "photo1";

            }
            else if(strPhoto1 == "" && !PageType.equals("NewRequestMedical"))
            {
                Toast.makeText(getApplicationContext(), "there is no attachment on this request",
                        Toast.LENGTH_SHORT).show();
            }else {
                Intent i = new Intent(MedicalClaim.this, DetailPhotoMedical.class);

               /* i.putExtra("photo", strPhoto1);*/
                Bundle extras = new Bundle();
                extras.putString("photo", strPhoto1);
                extras.putString("photoNo","photo1");
                extras.putString("PageTypeRequest",PageType);
                i.putExtras(extras);
                startActivityForResult(i,100);
                //i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);


            }
        }
    });

    ivAttachment2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(strPhoto2 == "" && PageType.equals("NewRequestMedical")) {
                Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
                photoTemp = "photo2";
            }
            else if(strPhoto2 == "" && !PageType.equals("NewRequestMedical"))
            {
                Toast.makeText(getApplicationContext(), "there is no attachment on this request",
                        Toast.LENGTH_SHORT).show();
            }
            else
            {
            Intent i = new Intent(MedicalClaim.this, DetailPhotoMedical.class);
           /* i.putExtra("photo", strPhoto2);
            i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(i);*/

                Bundle extras = new Bundle();
                extras.putString("photo", strPhoto2);
                extras.putString("photoNo","photo2");
                extras.putString("PageTypeRequest",PageType);
                i.putExtras(extras);
                startActivityForResult(i,100);
             }
        }
    });
    ivAttachment3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(strPhoto3 == "" && PageType.equals("NewRequestMedical")) {
                Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
                photoTemp = "photo3";
            }
            else if(strPhoto3 == "" && !PageType.equals("NewRequestMedical"))
            {
                Toast.makeText(getApplicationContext(), "there is no attachment on this request",
                        Toast.LENGTH_SHORT).show();
            }else
            {
                Intent i = new Intent(MedicalClaim.this, DetailPhotoMedical.class);
                Bundle extras = new Bundle();
                extras.putString("photo", strPhoto3);
                extras.putString("photoNo","photo3");
                extras.putString("PageTypeRequest",PageType);
                i.putExtras(extras);
                startActivityForResult(i,100);
                /*i.putExtra("photo", strPhoto3);
                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);*/
            }
        }
    });
    ivAttachment4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(strPhoto4 == "" && PageType.equals("NewRequestMedical")) {
                Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
                photoTemp = "photo4";
            }
            else if(strPhoto4 == "" && !PageType.equals("NewRequestMedical"))
            {
                Toast.makeText(getApplicationContext(), "there is no attachment on this request",
                        Toast.LENGTH_SHORT).show();
            }else
            {
                Intent i = new Intent(MedicalClaim.this, DetailPhotoMedical.class);
                Bundle extras = new Bundle();
                extras.putString("photo", strPhoto4);
                extras.putString("photoNo","photo4");
                extras.putString("PageTypeRequest",PageType);
                i.putExtras(extras);
                startActivityForResult(i,100);
               /* i.putExtra("photo", strPhoto4);
                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);*/
            }
        }
    });
    ivAttachment5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(strPhoto5 == "" && PageType.equals("NewRequestMedical")) {
                Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
                photoTemp = "photo5";
            }
            else if(strPhoto5 == "" && !PageType.equals("NewRequestMedical"))
            {
                Toast.makeText(getApplicationContext(), "there is no attachment on this request",
                        Toast.LENGTH_SHORT).show();
            }
            else{
                Intent i = new Intent(MedicalClaim.this, DetailPhotoMedical.class);
                Bundle extras = new Bundle();
                extras.putString("photo", strPhoto5);
                extras.putString("photoNo","photo5");
                extras.putString("PageTypeRequest",PageType);
                i.putExtras(extras);
                startActivityForResult(i,100);
               /* i.putExtra("photo", strPhoto5);
                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);*/
            }
        }
    });

now i just make 10 imageviews and declare all. which is not dynamic. and i convert the photo to bitmap and it will be send to database.

is there any way or library that i can use?

1

There are 1 best solutions below

1
On

You can have a "Add Image" button like below instead of having multiple images. enter image description here

Then on "add image" button click you can hit gallery intent with startactivityforresults() for getting the images and onAcivityResult() you can create imageView and add it to container and upload the same.

    public void addImageView(Context context, View container){
       ImageView imgNew = new ImageView(context);
       //add layout params & set image
       container.addView(newImage);
       // start service to upload the image
    }