Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get dynamically generated image name in android

Tags:

android

i want to get image name on click of image which is created dynamically......

 for(int i=0;i<list.size();i++)
    {
        final ImageButton b=new ImageButton(this);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(70, 70);
        layoutParams.setMargins(5, 5, 0, 0); // left, top, right, bottom
        b.setLayoutParams(layoutParams);
        Resources res = getResources();
        int resourceId = res.getIdentifier("_"+list.get(i).toString(), "drawable", getPackageName() );
        b.setImageResource( resourceId );
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
              //at here i want to get the selected image name


            }
        });
        rowoptions.addView(b);
    }    
like image 990
Rohit Avatar asked Feb 25 '13 12:02

Rohit


1 Answers

Set the image's name as the ImageButton tag. You can then retrieve it in onClick(). Use setTag() and getTag() for this:

    String imageName = "_" + list.get(i).toString();
    int resourceId = res.getIdentifier(imageName, "drawable", getPackageName() );
    b.setTag(imageName);
    b.setImageResource(resourceId);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
          //at here i want to get the selected image name
          String clickedImageName = (String)v.getTag();

        }
    });
like image 90
Geobits Avatar answered Oct 14 '22 11:10

Geobits