BrenDonie
BrenDonie

Reputation: 85

Taking Pictures and Display it on Multiple ImageView

I don't know what's proper title for my case. Since I don't even know what it's called. Basically I have this EditText and Button, where I can put number in EditText of how many ImageView I want to display on my app. Let say, I write 3 then it will display 3 ImageViews on Layout and it works just fine.

But, I want to separate each ImageView where when I click one of Imageview it will open Camera and display the result on ImageView that's just been clicked.

Is it possible? Below is my code of how I add ImageView to the layout

btnShelter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            int no = Integer.parseInt(jml_shelter.getText().toString());

            if(jml_shelter.getText().toString().equals("")){
                Toast.makeText(SiteActivity.this,"Masukkan Jumlah Shelter",Toast.LENGTH_SHORT).show();
            }else{
                container.removeAllViews();
                for (int i = 1; i <= no; i++) {
                    final View addView = layoutInflater.inflate(R.layout.shelter_row, null);
                    txtV1 = (TextView) addView.findViewById(R.id.txtV1);
                    txtV1.setText(String.valueOf(i));
                    img1 = (ImageView) addView.findViewById(R.id.imgLantai);
                    container.addView(addView);
                }
            }
        }
    });

Upvotes: 0

Views: 39

Answers (1)

Rajan Kali
Rajan Kali

Reputation: 12953

Create a global ImageView which holds reference to selected ImageView and on click do as below

private ImageView selectedImageView;//global
//while adding image views
for (int i = 1; i <= no; i++) {
                    final View addView = layoutInflater.inflate(R.layout.shelter_row, null);
                    txtV1 = (TextView) addView.findViewById(R.id.txtV1);
                    txtV1.setText(String.valueOf(i));
                    addView.setonClickListener(new View.OnClickListener(){
                          @Override
                          public void onClick(View v){
                            selectedImageView = (ImageView)v.findViewById(R.id.imgLantai);      
                           //Launch camera intent
                          }
                    });
                    container.addView(addView);
                }

Then in onActivityResult from Camera set image data to selectedImageView

Upvotes: 1

Related Questions