samedi 27 juin 2015

countup and countdown in a loop. android

I want to count from 1 till 15 for example and go from 15 till it reaches 1 and repeats the process... may you please help me complete? I tried everything. Instead of if, I tried While, it gave random numbers for some reason.

Here's my code:

int counter = 0;
int total = 15;
final Timer c=new Timer();
        c.scheduleAtFixedRate(new TimerTask() {         
                @Override
                public void run() {

                if (counter < total && timerHasStarted  ) {


                    runOnUiThread(new Runnable()

                    {
                        @Override
                        public void run()
                        {
                           number.setText("" +counter );
                            counter++;    

                        }
                    });
                }
                 if (counter == 15 && timerHasStarted  ) { {
                     runOnUiThread(new Runnable()

                        {
                            @Override
                            public void run()
                            {
                               number.setText("" +counter );
                               counter--;    

                            }
                        });

                }

                }

Aucun commentaire:

Enregistrer un commentaire