1

After reading dozens of Q&A regarding the subject, I couldn't find an answer to this situation.

In my app, the user is dragging different icons from one table layout to another (the bottom table layout is sort of an "icon keyboard").

After dragging the icon into the upper table (it replaces the placeholder image there), the user should be able to keep on dragging it there to different locations.

Right now, the first part work great. I'm able to drag the icons from the "keyboard" into the upper table layout. but for some reason, when I try to drag it between the different locations inside this layout, the imageView that should change into the icon, stays with the same drawable (the placeholder).

public boolean onDrag(View v, DragEvent event) {
      ImageView imageOnBoard = (ImageView) v;
      switch (event.getAction()) {
      case DragEvent.ACTION_DRAG_STARTED:
               break;
      case DragEvent.ACTION_DRAG_ENTERED:   
          imageOnBoard.setBackgroundColor(0xFF00FF00);

        break;
      case DragEvent.ACTION_DRAG_EXITED:   
          imageOnBoard.setBackgroundColor(color.transparent);  
          imageOnBoard.setOnTouchListener(null);
        break;
      case DragEvent.ACTION_DROP:        


         imageOnBoard.setBackgroundColor(color.transparent);
        TableRow tr1 =  (TableRow) imageOnBoard.getParent();
        int r=tr1.indexOfChild(imageOnBoard);
        TableLayout tl1  = (TableLayout) tr1.getParent();
        int c=tl1.indexOfChild(tr1);


        ImageView strategyIcon = (ImageView) (event.getLocalState()); 
        TableLayout source = (TableLayout) strategyIcon.getParent().getParent();
        if (source.equals((TableLayout) findViewById(R.id.tableLayout)))  
        {

            Drawable draw = strategyIcon.getDrawable();
            imageOnBoard.setImageDrawable(draw);

        }
        else
        {

             Drawable draw = strategyIcon.getDrawable();
            imageOnBoard.setImageDrawable(draw);

        }


        imageOnBoard.setVisibility(View.VISIBLE);
        imageOnBoard.setOnTouchListener(new MyTouchListener());

        break;
      case DragEvent.ACTION_DRAG_ENDED:  

            break;

      default:
        break;
      }
      return true;
    }

as you can see, i devided the DRAG_DROP action into the two cases (dragging from the "keyboard" and dragging inside the upper layout. I tested it, and it does go into the different cases, but the drawable object seems to be "unupdated".

Please help!

I now understand that the problem is while extracting the Drawable (getDrawable) for the second time, from the upper layout, I don't receive the current drawable, but the original one. How can I handle this?

Maoritzio
  • 387
  • 1
  • 6
  • 8

5 Answers5

2

I met same problem, when I set Image to a ImageView using image1.setImageDrawable(add); sometimes it doesn't work, later I used glide to set image: e.g. Glide.with(Activity.this).load(R.drawable.add).into(image1); then it works fine

Michael
  • 646
  • 6
  • 16
1

I run into the same problem and my solution was to set a LayoutParams of the image before the "setImageDrawable". Apparently as the drawable has no size (specially, if it is a custom drawable or some icon) is showed as zero length in width and height in the screen. The solution is therefore:

SemiCircleDrawable d = new SemiCircleDrawable(...); // just a custom drawable

myImage.setLayoutParams(newLinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
myImage.setImageDrawable(d);
Ariel Teve
  • 23
  • 2
0

Isn't it just a simple error ? here you get the "draw" Drawable, but you don't do anything with it. Maybe replace :

if (source.equals((TableLayout) findViewById(R.id.tableLayout)))  
        {
            //imageOnBoard.setImageResource(strategyIcon.getId());
            Drawable draw = strategyIcon.getDrawable();
            imageOnBoard.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));

        }

By :

if (source.equals((TableLayout) findViewById(R.id.tableLayout)))  
            {
                Drawable draw = strategyIcon.getDrawable();
                imageOnBoard.setImageDrawable(draw);

            }
Andros
  • 4,069
  • 1
  • 22
  • 30
0

The problem was acctualy with the getDrawable, which provided the old Drawable. this was fixed using Tags:

if (source.equals((TableLayout) findViewById(R.id.tableLayout)))   //in case this icon is dragged from the board
        {


            Drawable draw = (Drawable) strategyIcon.getTag();
            imageOnBoard.setImageDrawable(draw);
            imageOnBoard.setTag(draw);

        }




        else                                                           //in case this icon is dragged from the menu
        {
            Drawable draw = strategyIcon.getDrawable();
            imageOnBoard.setImageDrawable(draw);

            imageOnBoard.invalidateDrawable(draw);
            imageOnBoard.setTag(draw);

        }
Maoritzio
  • 387
  • 1
  • 6
  • 8
-1

as setImageDrawable() method is deprecated in api level 16 thus this method will not work. Instead you can use the following method

@SuppressWarnings("deprecation")
    private void setRes(ImageView iv,Drawable drawable){
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
            iv.setBackground(drawable);
        else
            iv.setBackgroundDrawable(drawable);
    }

for ref # http://developer.android.com/reference/android/view/View.html#setBackground(android.graphics.drawable.Drawable)

dinesh sharma
  • 3,312
  • 1
  • 22
  • 32
  • That didn't solve the problem. besides, the method does work on the second case, so why shouldn't it work on the first one? – Maoritzio Dec 18 '13 at 10:11
  • this depends on the device if your device is above api level 15 then first case will execute else second case will execute. – dinesh sharma Dec 18 '13 at 10:58
  • What I meant was that the else statement on my code (not in the answer you suggested) is executed and works fine, and it is equivalent to the "if" statement, so why shouldn't they both work? – Maoritzio Dec 18 '13 at 13:01
  • make 2 emulator one with android OS 2.2 and another with 4.4 then try running in both the emulator in 2.2 else part will execute and in 4.4 if part will execute. because above api level 15 setBackgroundDrawable() method is not supported. – dinesh sharma Dec 18 '13 at 13:05
  • It is running, but the problem is it's not assigning the Drawable I want to the imageView. is there something else wrong here? – Maoritzio Dec 18 '13 at 13:17
  • check whether imageview is initialized or not and other thing is transparency that may be affecting – dinesh sharma Dec 18 '13 at 13:33
  • 1
    ImageView.setImageDrawable is not deprecated – Softlion Aug 16 '18 at 07:24