Skip to content Skip to sidebar Skip to footer

How To Get The Particular Sub View Id From The View In Android?

I created a dynamic view that contains FrameLayout, and it contains ImageViews. Now, when I touch the particular image on frame layout, I want know the ID of the ImageView. So, her

Solution 1:

you have to set ontouch listener to your image:

yourImage.setOnTouchListener(newOnTouchListener() {

        @OverridepublicbooleanonTouch(View v, MotionEvent arg1) {
            // this is your id you can pass it
            v.getId()
            // TODO Auto-generated method stubreturnfalse;
        }
    });

Solution 2:

If you only want to identify the view which is touched, you can add listeners to your dynamic image views also. Like below

spotimage.setOnClickListener(newOnClickListener() {`           
                @OverridepublicvoidonClick(View v) {

                }
            });

and in the onClick methord you can write code specific to each image view, or if you strictly want to set the id for image views, you can use spotimage.setId(1) Id can be any integer value, but you have to make sure no conflict will occur with other id values. and in any listener like OnClickListenet, you can check the image view id byint temp = view.getId();

Post a Comment for "How To Get The Particular Sub View Id From The View In Android?"