user710502
user710502

Reputation: 11471

Change Image on runtime

I am able to now change an image when pressing OnTouch, for example I press image1 make it invisible and show the one i have underneath it...

Now i want to change an image on runtime, basically I have an array if array[i] = 5; then i want to make image5 invisible?

How can I do this?, I tried it with a

try{
     Thread.sleep(2000);
     }
catch
  {
  }

and it blacks out the screen and then shows the backgroung after 2 seconds but i never see the image get invisible;

i would like to be able to do something like this

for(int i=0;i < array.length; i++)
{
 if(array[i] == 1)
     {
          try
          {
         Thread.sleep(1000);
         ImageView image = (ImageView)findById(myImage01);
         image.setVisibility(View.INVISIBLE);      
         Thread.sleep(1000);
         image.setVisibility(View.VISIBLE);  

           }  
         catch(InterruptedException e) 
            {
            }
       }

}

You get the idea... right now if i do this it will black out the screen and then return to the main layout and do nothing :(

Thank you in advance

By the way I am new in Android (please consider :( )

Upvotes: 0

Views: 2701

Answers (2)

devisnik
devisnik

Reputation: 256

Thread.sleep() is a bad idea because it halts the thread. So if it is the UI-thread, it can not refresh the screen anymore. That's probably what you observe. Use something like

new Handler().postDelayed(new Runnable() { 
  void run() {
      image.setVisibility(View.VISIBLE);
  }
}, 1000);

Upvotes: 1

artifex
artifex

Reputation: 127

Lets see if i understand this correctly. You are trying to show an image by turning an image, that is placed above it, invisible?

It seems that the easier way would be to change the image of the actual imageview your are turning invisible.

image.setImageResource(R.drawable.<image>)

Where you change to the name of the image placed inside your res/drawable-hdpi/mdpi/ldpi folder.

Upvotes: 2

Related Questions