1

Now the image is changed as soon as I click on.

I would like to make this process AUTOMATIC (ex. every second)

this is my actually code

public void onClick(View v) {
    switch (v.getId()) {
        case R.id.imageView:
            foto.setVisibility(View.INVISIBLE);
            foto1.setVisibility(View.VISIBLE);
            break;
        case R.id.imageView2:
            foto1.setVisibility(View.INVISIBLE);
            foto.setVisibility(View.VISIBLE);
    }
}
Hugo Gresse
  • 17,195
  • 9
  • 77
  • 119

5 Answers5

2

Have a look at ViewFlipper

http://developer.android.com/reference/android/widget/ViewFlipper.html

You can set the delay using setFlipInterval(int) or in the XML using android:flipInterval

0

You should use a Handler with `postDelayed'. See this answer for some code.

Also, you can try using 'ImageSwitcher' instead of two 'ImageView's

Community
  • 1
  • 1
Evripidis Drakos
  • 872
  • 1
  • 7
  • 15
0

Try this:

boolean isFirstVisible;
long millis;        

while(true) {
    millis = System.currentTimeMillis();

    if (isFirstVisible) {
        foto1.setVisibility(View.INVISIBLE);
        foto.setVisibility(View.VISIBLE);
        isFirstVisible = false;
    } else {
        foto.setVisibility(View.INVISIBLE);
        foto1.setVisibility(View.VISIBLE);
        isFirstVisible = true;
    }

    Thread.sleep(1000 - millis % 1000);
}   
GIGAMOLE
  • 1,274
  • 1
  • 11
  • 17
0

maybe use ViewSwitcher (in your casae ImageSwitcher should work) and switch Views using Handler?

private static final int delay=1000; //ms
Handler h = new Handler();
Runnable r = new Runnable(){
    public run(){
        //viewSwitcher.showNext();
        //exampole of swtiching
        h.postDelayed(r, delay);
    }
}
h.postDelayed(r, delay);

to stop this loop use h.removeCallbacks(r)

you may also use viewSwitcher.postDelayed

snachmsm
  • 17,866
  • 3
  • 32
  • 74
0

Yo can have a periodic task to do it:

ScheduledExecutorService scheduleTaskExecutor = Executors.newScheduledThreadPool(1);

scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
  public void run() {
    runOnUiThread(new Runnable() {
      public void run() {
        if (foto.getVisibility == View.VISIBLE) {
            foto.setVisibility(View.INVISIBLE);
            foto1.setVisibility(View.VISIBLE);
        } else {
            foto.setVisibility(View.VISIBLE);
            foto1.setVisibility(View.INVISIBLE);
        }
      }
    });
  }
}, 0, 1, TimeUnit.SECONDS);
danips
  • 26
  • 1
  • 3
  • It's better practice to use a Handler rather than a separate thread and then call runOnUiThread when dealing with timed events. – Sam Bains Feb 04 '15 at 14:44