DomeWTF
DomeWTF

Reputation: 2420

setBackgroundResource doesn't set the image

    Handler hnd = new Handler() {

    @Override
    public void handleMessage(Message msg) {

        int id = sequence.get(msg.arg1);

        if(msg.arg1 % 2 == 0) {
            sq.get(id-1).setBackgroundResource(R.drawable.square_show);
        } else {
            sq.get(id-1).setBackgroundResource(R.drawable.square);
        }
    }
};

@Override
public void onResume() {
    super.onResume();

    Thread background = new Thread(new Runnable() {
        public void run() {
            try {
                for(int i = 0; i < sequence.size()-1; i++) {
                    record_tv.setText(""+i);
                    Thread.sleep(200);
                    Message msg = hnd.obtainMessage();
                    msg.arg1 = i;
                    msg.sendToTarget();
                }
            } catch(Throwable t) {

            }
        }
    });

    background.start();
}

[CODE UPDATED] now it goes through the first loop and stops

do you have any idea why the code in the first runOnUiThread gets executed but it doesn't do what i want?

what i want is: change the image to "square", wait 2 seconds, change the image to "square_show", wait 2 secs and repeat the loop

i've been struggling for an hour now...

Upvotes: 0

Views: 2537

Answers (5)

Mohammad Imran
Mohammad Imran

Reputation: 3273

Try this,It will work:

public void show(final int size) {
    Thread thrd = new Thread(new Runnable() {

        @Override
        public void run() {
            for (int i = 0; i <= size - 1; i++) {
                id = (Integer) sequence.get(i);
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        sq.get(id - 1).setBackgroundResource(
                                R.drawable.square_show);
                    }
                });
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        sq.get(id - 1).setBackgroundResource(
                                R.drawable.square);
                    }
                });
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    thrd.start();
}

Upvotes: 0

iceman
iceman

Reputation: 826

Because the resource changes in the UI thread and you are sleeping your background thread. The UI thread is running normally. Use handlers:

public class MainActivity extends Activity {

Button b;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b = (Button) findViewById(R.id.button1);
}
Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {

        if (msg.arg1 % 2 == 0) {
            b.setBackgroundResource(R.drawable.analytic_icon);
        } else {
            b.setBackgroundResource(R.drawable.ic_launcher);
        }
    }
};

@Override
public void onResume() {
    super.onResume();

    Thread background = new Thread(new Runnable() {
        public void run() {
            try {
                for (int i = 0; i < 20; i++) { 
                    Thread.sleep(2000);
                    Message msg = handler.obtainMessage(); 
                    msg.arg1 = i;
                    msg.sendToTarget();
                }
            } catch (Throwable t) {
                // just end the background thread
            }
        }
    });

    background.start();
}
}

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

I would suggest you to use a handler

int drawablebkg[] ={R.drawable.ic_launcher,R.drawable.icon};
Handler m_handler;
Runnable m_handlerTask ; 
ImageView iv;
iv = (ImageView)findViewById(R.id.imageView1);
m_handler = new Handler(); 
   m_handlerTask = new Runnable()
    {
         @Override 
         public void run() {
             iv.setImageResource(android.R.color.transparent); 
            if(i<2)
            {

              ivsetBackgroundResource(drawablebkg[i]);
              i++;
            }
            else 
              {
                  i=0;
              }
              m_handler.postDelayed(m_handlerTask, 2000);
         }
    };
    m_handlerTask.run();  

In onPause() of your activity

 @Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();

    //_t.cancel();
    m_handler.removeCallbacks(m_handlerTask);
}  

Another way

    iv= (ImageView)findViewById(R.id.imageView1);
    AnimationDrawable animation = new AnimationDrawable();
    animation.addFrame(getResources().getDrawable(R.drawable.ic_launcher), 2000);
    iv.setImageResource(android.R.color.transparent); 
    animation.addFrame(getResources().getDrawable(R.drawable.icon), 2000);

    animation.setOneShot(false);

    iv.setBackgroundDrawable(animation);
    //set setBackgroundDrawable(animation) is decprecreated i guess. not sure in which api

    // start the animation!
    animation.start();

Another way

Define background.xml in drawable folder

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/ic_launcher" android:duration="2000" />
<item android:drawable="@drawable/icon" android:duration="2000" />
</animation-list>

I your activity onCreate();

ImageView iv = (ImageView)findViewById(R.id.imageView1);
iv.setBackgroundResource(R.drawable.background);

AnimationDrawable animation= (AnimationDrawable)loadingRaven.getBackground();
loadingRaven.setImageResource(android.R.color.transparent); 
animation.start();

Note to stop the animation you need to call animation.stop()

Upvotes: 0

prvn
prvn

Reputation: 916

public void show(int size) {

    // CICLE THROUGH EACH SQUARE

    for(int i = 0; i <= size-1; i++) {          

        Thread thrd = new Thread() {
            public void run() {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                                           sq.get(id-1).setImageResource(R.drawable.square_show);         
                               //     System.out.println("1st.........."); 
                       try {
                        sleep(2000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                                         sq.get(id-1).setImageResource(R.drawable.square);
                  //     System.out.println("2nd..........");
                       try {
                        sleep(2000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    }
                });




            }
        };

        thrd.start();
    }
}

Upvotes: 0

Jayeshkumar Sojitra
Jayeshkumar Sojitra

Reputation: 2511

You can easily set image using following code.

sq.get(id-1).setImageResource(R.drawable.square_show);
sq.get(id-1).setImageResource(R.drawable.square);

Upvotes: 1

Related Questions