Question

i´m making an app that shows random sentences when you press a botton, this process is pefect, part of my code is:

 botonok.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {
            Resources res = getResources();
            myString = res.getStringArray(R.array.myArray); 
            String a = myString[rgenerator.nextInt(myString.length)];
            TextView elemento1 = (TextView) findViewById(R.id.elemento1);
            elemento1.setText(a);

...

the problem, i need these sentence appear gradually, for example, the 1st sentence "elemento1" should appears just when you click the botton, the 2nd sentence, half second later, the 3rd sentece, one second later.. i have 4 sentences, as you can see, they are from an array.

Thanks for helping

Was it helpful?

Solution

Define a Timer after the class.

static volatile int time; Timer t = new Timer();

now edit your function like this

  botonok.setOnClickListener(new OnClickListener() 
{
    public void onClick(View v) 
    {
        Resources res = getResources();
        myString = res.getStringArray(R.array.myArray); 
        String a = myString[rgenerator.nextInt(myString.length)];
        time=1;

        t.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {

            runOnUiThread(new Runnable() {


                public void run() {
        TextView elemento1 = (TextView) findViewById(R.id.elemento1);
        if(time==0||time==2||time==4||time==6)
        elemento1.setText(a);
        time=time+1;
        }
                }

            });

        }
    }, 100, 1000);

OTHER TIPS

thank you, i just wrote your code and it work!! i had to make some changes, coz there are more textView, and my code is this:

botonok.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {
time=1; 
            t.scheduleAtFixedRate(new TimerTask() {
                public void run() {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            if(time==2){
                      Resources res = getResources();
         myString = res.getStringArray(R.array.myArray);    
         String a = myString[rgenerator.nextInt(myString.length)];          
     TextView elemento1 = (TextView) findViewById(R.id.elemento1);
            elemento1.setText(a);
                            }
                            //
                        if(time==4){
                      Resources res = getResources();
          myString = res.getStringArray(R.array.myArray2); 
         String b = myString[rgenerator.nextInt(myString.length)];
     TextView elemento2 = (TextView) findViewById(R.id.elemento2);
            elemento2.setText(b);

// here i write the same process for (elemento3 and elemento4) // then i write the last line:

}time=time+1; 
                        }
                    });
        }
}, 100, 1000)   ;
            ;}
});}}

but, there is a little mistake, the first time i touch the botton, the time is 2,4,6,8 but avery time i touch it, it gets faster, i wrote a conditional "countTouch++" but it didn´t work :(

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top