Android Ui thread in loop

2.8k Views Asked by At

PROBLEM


I have nearly 20 TextView and I need to have do something to make their background color change dynamically.

For example: all TextView have same default background color then after 5 sec first one's will turn red where others still same then after 5 more sec passed first TextView's background color will turn to default and second TextView's background color will be turn to red and it will go on like this.

I tried to put them in for loop in thread and handler but they changed all together. What should I do ?

I have tried this code

Thread color=new Thread() {
    public void run() {
        for(int i=2131230724;i<=2131230742;i++){
            TextView currentText = (TextView) findViewById(i);
            currentText.setBackgroundColor(Color.RED);
            try {
                sleep(2000);
                currentText.setBackgroundColor(Color.TRANSPARENT);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    };

then I used

mHandler.post(color);

SOLUTION


I found a solution for my problem and sharing if someone else needed too.

Runnable myhandler=new Runnable() {
    int id=2131230724;//First textView id
    @Override

    public void run() {

        // TODO Auto-generated method stub
        if(id==2131230724){
            TextView currentText=(TextView)findViewById(R.id.egzersiz01_kelime1);
            currentText.setBackgroundColor(Color.RED);
            id++;
        }
        else if(id==2131230725){
            TextView currentText=(TextView)findViewById(R.id.egzersiz01_kelime2);
            currentText.setBackgroundColor(Color.RED);
            TextView PreText=(TextView)findViewById(R.id.egzersiz01_kelime1);
            PreText.setBackgroundColor(Color.TRANSPARENT);
            id++;
        }//And all other id's to else if as same as mine.


mHandler.postDelayed(myhandler, delay);

and in onCreate()

myhandler.run();

I have tried to use for loop to make it easy but it crashed and I had to write them all. Thanks for helping...

1

There are 1 best solutions below

1
On

You can put the params to AsynTask like this, and then

new changeColor().execute(max);


private class changeColor extends AsyncTask<Integer,Integer,Void>{
    int max,cur;
    @Override
    protected void onPreExecute() {
        max=cur=0;
        super.onPreExecute();
    }
    @Override
    protected Void doInBackground(Integer... number) {
        int max=number[0];
        int cur=1;
        if(max>0)
        while(true ){/*or other condition else*/
            try {
                Thread.sleep(5000);//Sleep 5000s before make change
            } catch (InterruptedException e) {
                Log.i("TAG","InterruptedException");
            }
            publishProgress(cur);
            if(cur==max)
                cur=1;
            else
                cur++;
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
            changeColorTextView(values[0]);
        super.onProgressUpdate(values);
    }
    private void changeColorTextView(int textViewId){
        switch(textViewId){
        case textId1:{
            //do some thing here like:
            //textView2.setBackGroundColor(R.color.RED);
            //textView1.setBackGroundColor(default);
            break;
        }
        //....
        }
    }


}