Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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

Solution 2:

public void show(int size) {

// CICLE THROUGH EACH SQUAREfor(int i = 0; i <= size-1; i++) {          

        Thread thrd = new Thread() {
            publicvoidrun() {
                runOnUiThread(new Runnable() {

                    @Override
                    publicvoidrun() {
                                           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();
    }
}

Solution 3:

Solution 4:

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 
         publicvoidrun() {
             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);
    AnimationDrawableanimation=newAnimationDrawable();
    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-listxmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="false"><itemandroid:drawable="@drawable/ic_launcher"android:duration="2000" /><itemandroid:drawable="@drawable/icon"android:duration="2000" /></animation-list>

I your activity onCreate();

ImageViewiv= (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()

Solution 5:

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

publicclassMainActivityextendsActivity {

Button b;

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b = (Button) findViewById(R.id.button1);
}
Handlerhandler=newHandler() {
    @OverridepublicvoidhandleMessage(Message msg) {

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

@OverridepublicvoidonResume() {
    super.onResume();

    Threadbackground=newThread(newRunnable() {
        publicvoidrun() {
            try {
                for (inti=0; i < 20; i++) { 
                    Thread.sleep(2000);
                    Messagemsg= handler.obtainMessage(); 
                    msg.arg1 = i;
                    msg.sendToTarget();
                }
            } catch (Throwable t) {
                // just end the background thread
            }
        }
    });

    background.start();
}
}

Post a Comment for "Setbackgroundresource Doesn't Set The Image"