2

I'm relatively new to android and I'm working on a horizontal scroll view with drag and drop functionality. Right now everything is working perfectly fine I can drag the image and drop it in the drop zone with count of failed and successful drops. For now the image that is being dragged and its shadow looks the same. What I want is that when I drag an image the shadow that appears for that image is an image from drawables that I select.

Here is the code for shadow builder:

 void dragAndDropImage(int imageId)
{
   // int drawableid = getResources().getIdentifier("drawable/a1", "id", getPackageName());
    final ImageView drag = (ImageView)findViewById(imageId);

    drag.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent arg1) {
            // TODO Auto-generated method stub
            ClipData data = ClipData.newPlainText("", "");
            View.DragShadowBuilder shadow = new View.DragShadowBuilder(drag);//I want the drawable id here View.DragShadowBuilder(drawableid)
            v.startDrag(data, shadow, null, 0);
            return false;
        }
    });

I want to get the id of one particular image from drawables and then I can use it in View.DragShadowBuilder(id of the image from drawables) to change the image of the shadow. Any help in this regard is appreciated.

JBJ
  • 268
  • 2
  • 13

5 Answers5

1

It will be something like:

R.drawable.resourcename

Make sure you don't have the Android.R namespace imported as it can confuse Eclipse (if thats what you're using).

If that doesn't work, you can always use a context's getResources method ...

Drawable resImg = this.context.getResources().getDrawable(R.drawable.resource);

Where this.context is intialised as an Activity, Service or any other Context subclass.

Update:

If it's the name you want, the Resources class (returned by getResources()) has a getResourceName(int) method, and a getResourceTypeName(int)?

Update 2:

The Resources class has this method:

public int getIdentifier (String name, String defType, String defPackage) Which returns the integer of the specified resource name, type & package.

Naitik
  • 960
  • 11
  • 31
1

(I have been seeing this post getting more views every time I log in and I solved this a long time ago but I'm going to post the solution in hopes that it helps someone else.) In my case I had multiple images in Drawable that I wanted to use therefore

 for (int j=1; j<10; j++)
    {
        setImages("drawable/img" +j);
        //  SetPopup(drag);
    }

void setImages(final String draw)
{
    int id12 = getResources().getIdentifier(draw, "id", "your_package_name");
    drag1.setImageResource(id12);

I just posted the part of code that helped me access different images directly from drawables and use them as drag images on multiple imageViews.

JBJ
  • 268
  • 2
  • 13
0
String uri = "@drawable/yourdrawable";
int imageResource = getResources().getIdentifier(uri, null, getPackageName());

Can you try this?

Febi M Felix
  • 2,611
  • 1
  • 8
  • 13
  • Here is the error - DragShadowBuilder (android.view.View) in DragShadowBuilder cannot be applied to (int) – JBJ Jan 05 '17 at 07:11
  • Did you come across this link http://stackoverflow.com/questions/32198394/android-how-to-use-dragshadowbuilder This might help you in implementing the DragShadowBuilder. – Febi M Felix Jan 05 '17 at 07:17
  • The thing is that the shadow builder is working fine already. What I want is to somehow access drawables and use them to build shadows on click. – JBJ Jan 05 '17 at 07:40
0

Try this,

final ImageView drag = (ImageView)findViewById(R.id.imageId);
drag.setImageResource(R.drawable.a1);
View.DragShadowBuilder shadow = new View.DragShadowBuilder(drag);
santosh kumar
  • 2,612
  • 1
  • 12
  • 24
  • It is changing all the images to that particular shadow image whereas its supposed to be where images remain the same only their shadow (it appears when they are dragged) changes. – JBJ Jan 05 '17 at 08:47
  • I did not understand can you please explain ? – santosh kumar Jan 05 '17 at 08:51
  • The imageId is the id of the actual image in linear layout of the horizontal scroll view . Basically it is the id of the item being dragged or you can say it is to identify what image view is to be or being dragged. When I use *final ImageView drag = (ImageView)findViewById(R.id.imageId); drag.setImageResource(R.drawable.a1);* it changed my original images as well whereas I want original images to stay the same and only want the shadow to change. – JBJ Jan 05 '17 at 09:02
0

Assume you have several drawables in your Android resource folder and what to iterate over them. You use a certain naming schema and would like to use this to determine the resources ID’s.

The following steps shows how to get a resource ID based on the resource name:

   // Name of resource 
  //Type
 // Package of the app

 int identifier = getResources().getIdentifier("pic1", "drawable", "android.demo");
        ImageView image = (ImageView) findViewById(R.id.imageView1);
        image.setImageResource(identifier);