0

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, here are my questions:

  1. How can I set the ID for the ImageView?

  2. How can I recognize particular ImageView is touched?

Here is the sample snippet of the code:

for (int j = 0; j < _pageslist.size(); j++) {
    FrameLayout frame = new FrameLayout(HLActivity.this);
    LayoutParams params = new FrameLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    frame.setLayoutParams(params);

    ImageView mainimage = new ImageView(HLActivity.this);

    mainimage.setImageBitmap(ReusableMethods.getBitmapFromURL(_pageslist.get(j)
            .getThumbnail().toString()));
    mainimage.setScaleType(ScaleType.FIT_XY);
    mainimage.setLayoutParams(params);

    frame.addView(mainimage, params);

    if (_pageslist.get(j).isHasspots()) {
        System.out.println(_pageslist.get(j).isHasspots());
        System.out.println(_pageslist.get(j).getSPOTS());

        ArrayList<Hotspot> hotspots_array = _pageslist.get(j).getSPOTS();
        for (int i = 0; i < hotspots_array.size(); i++) {
            Hotspot hotspot = hotspots_array.get(i);
            System.out.println("hotspot :: " + hotspot.getType());

            ImageView spotimage = new ImageView(HLActivity.this);
            spotimage.setBackgroundColor(Color.parseColor("#88676767"));

            float startx, starty, endx, endy;
            startx = (float) (Float.parseFloat(hotspot.getX()) * ivw) / 100;
            starty = (float) (Float.parseFloat(hotspot.getY()) * ivh) / 100;
            endx = (float) ((Float.parseFloat(hotspot.getX()) + 
                    Float.parseFloat(hotspot.getWidth())) * ivw) / 100;
            endy = (float) ((Float.parseFloat(hotspot.getY()) + 
                    Float.parseFloat(hotspot.getHeight())) * ivh) / 100;

            params = new FrameLayout.LayoutParams(
                    (int) ((Float.parseFloat(hotspot.getWidth()) * ivw)/100),
                    (int) ((Float.parseFloat(hotspot.getHeight()) * ivh)/100));

            params.leftMargin = (int) startx;
            params.topMargin = (int) starty;

            frame.addView(spotimage, params);
        }
    }
    _view.add(frame);
}

adapter = new ViewPagerAdapter(HLActivity.this, _view,
        _pageslist, ivw, ivh, getStatusBarHeight());
viewPager.setAdapter(adapter);
Andrew T.
  • 4,701
  • 8
  • 43
  • 62

2 Answers2

1

you have to set ontouch listener to your image:

yourImage.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent arg1) {
            // this is your id you can pass it
            v.getId()
            // TODO Auto-generated method stub
            return false;
        }
    });
NavinRaj Pandey
  • 1,674
  • 2
  • 26
  • 40
0

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(new OnClickListener() {`           
                @Override
                public void onClick(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 by
int temp = view.getId();

Useless
  • 81
  • 5