OnClick on inflated items

1.3k views Asked by At

In my activity.xml I have a LinearLayout. This LinearLayout contains an inflated layout consisting of four ImageViews. Implementation is something like this:

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_template);
View view = getLayoutInflater().inflate(R.layout.inflated_layout, linearLayout, false);
linearLayout.addView(view);

I want to implement an action if the user clicks on the image. However, nothing happens when I click on the image. This is my implementation for onclick event: v.getId() gives me -1 every time I click an image

view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Log.v("V.getID", String.valueOf(v.getId()));
            Log.v("Image 1 id", String.valueOf(R.id.iv_t_1));

            switch (v.getId()){

                case R.id.iv_t_1: //image 1 clicked
                    //action
                    break;

                case R.id.iv_t_2: //image 2 clicked
                    //action
                    break;

                case R.id.iv_t_3: //image 3 clicked
                    //action
                    break;

                case R.id.iv_t_4: //image 4 clicked
                    //action
                    break;

                default:
                    break;
            }
        }
    });

EDIT: The reason i don't want to set listeners on separate images is because the inflated layout is dynamic. It can contain any number from 1 to 4 images. If I have a layout of two images then the fourth imageView listener would return a nullptr exception

3

There are 3 answers

0
Phuc Tran On BEST ANSWER

You can find those images and check if it's not null before setting onClickListener.

ImageView img1 = (ImageView) view.findViewById(R.id.imageview1);
if (img1 != null) {
  img1.setOnClickListener(...);
}

ImageView img2 = (ImageView) view.findViewById(R.id.imageview2);
if (img2 != null) {
  img1.setOnClickListener(...);
}

...
0
Harish_N On

v.getId() is giving -1 because you have not set any ID to Root Element in the Layout inflated_layout. You set an ID to Root Element in "inflated_layout" then v.getId() will give you a valid ID.


View view = getLayoutInflater().inflate(R.layout.inflated_layout, linearLayout, false);
linearLayout.addView(view);

when you add click listener to the view you inflated above, you are adding click listener to the entire layout(inflated_layout) and not to the individual imageviews present inside inflated_layout. To add same click listener to all imageviews present inside inflated_layout do the following.

Create a private class in the class where you are inflating the inflated_layout.

 private class ClickListener implements View.OnClickListener{

    @Override
    public void onClick(View v) {
        Log.d("test","View Id="+v.getId());
    }

}

Then you get a reference to each imageview and set the click listener to each imageview as below.

    LinearLayout linearLayout=(LinearLayout) findViewById(R.id.ll_template);
    View view=getLayoutInflater().inflate(R.layout.inflated_layout, null);
    linearLayout.addView(view);

    ImageView iv1=(ImageView) view.findViewById(R.id.iv_t_1);
    ImageView iv2=(ImageView) view.findViewById(R.id.iv_t_2);
    ImageView iv3=(ImageView) view.findViewById(R.id.iv_t_3);
    ImageView iv4=(ImageView) view.findViewById(R.id.iv_t_4);

    ClickListener cl=new ClickListener();
    iv1.setOnClickListener(cl);
    iv2.setOnClickListener(cl);
    iv3.setOnClickListener(cl);
    iv4.setOnClickListener(cl);
0
Sanket Berde On

do this,

ImageView img1 = (ImageView) view.findViewById(R.id.imageview1);
img1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Log.v("V.getID", String.valueOf(v.getId()));
            Log.v("Image 1 id", String.valueOf(R.id.iv_t_1));

            switch (v.getId()){

                case R.id.iv_t_1: //image 1 clicked
                    //action
                    break;

                case R.id.iv_t_2: //image 2 clicked
                    //action
                    break;

                case R.id.iv_t_3: //image 3 clicked
                    //action
                    break;

                case R.id.iv_t_4: //image 4 clicked
                    //action
                    break;

                default:
                    break;
            }
        }
    });