Maoritzio
Maoritzio

Reputation: 437

setImageDrawable is not setting the current drawable

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?

Upvotes: 1

Views: 11036

Answers (5)

Ariel Teve
Ariel Teve

Reputation: 23

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);

Upvotes: 1

Michael
Michael

Reputation: 666

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

Upvotes: 2

Maoritzio
Maoritzio

Reputation: 437

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);

        }

Upvotes: 0

Andros
Andros

Reputation: 4069

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);

            }

Upvotes: 0

dinesh sharma
dinesh sharma

Reputation: 3332

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)

Upvotes: -1

Related Questions