Is it possible for a single button to change Imageview A to B, C, D... ? - Java/Android

Asked

Viewed 43 times

0

Is it possible to change the same button Imageview A to B, C and so on? In the same onClickListener?

My first attempt, knowing it wouldn’t work, was

mNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            escUm.setVisibility(View.INVISIBLE);
            escDois.setVisibility(View.VISIBLE);
            escDois.setVisibility(View.INVISIBLE);
            escTres.setVisibility(View.VISIBLE);

I also tried a Handler to apply a delay from one image to another. Obviously not what I wanted.

handler.postDelayed(new Runnable(){
                @Override
                public void run() {
                    escTres.setVisibility(View.VISIBLE);
                }
            }, 5000); */

Does anyone know how to proceed?

1 answer

1


As far as I understand it ,you want Imageview to act as a "slide show" by pressing a button.

For this you can use the Timer and Timertask class as follows.

Outside the onclick button create a Timer and declares Timertask.

Timer timer = new Timer();
TimerTask task;

In onclick you will instantiate Timertask and make the following code below.

task = new TimerTask() {
    @Override
    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // Código para a troca de imagens
            }
        });
    }
};
timer.scheduleAtFixedRate(task ,delay ,período);

In the exchange code I advise you to create an entire variable to control the images and I also suggest you take care of this method because it is independent of the main thread (that of the application).

  • Got it... Man, thank you so much! Before I thought that these 'slides' should be done in new activities until... I will follow your reply. Thanks!

  • But as I said ,be careful because Timer&timertask are independent of the main thread ,it is possible that even the user destroying the application they are running.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.