2

I have a button, and when i click to it, i make textView.setText() a very big text, 50000 symbols, and the interface stops for 3 seconds, how can i fix it?

  1. I tried to make it with Handle and thread, but it hasn`t helped.
  2. I tried to make textview.append(), but it also hasn`t helped.

                MainActivity.this.runOnUiThread(new Runnable() {
    
                @Override
                public void run() {
    
                    textText.append(rowItemHeader.getText().substring((endOfNews - 10000, endOfNews));
                }
            });
    

    Edit 1 no result

private class MyTask extends AsyncTask <Void, Void, String> { String str; TextView txt;

    MyTask(String str, TextView txt){
        this.str = str;
        this.txt = txt;

    }
    public String doInBackground(Void... args) {

        return str.substring(endOfNews - 10000, endOfNews);
    }

    public void onPostExecute(String myString) {
        // do your update here or you will get that error (the original thread one)
        txt.append(myString);
    }
}
  • What is not working? Is the variable `myString` correctly elaborated? – Enrichman Jun 25 '13 at 12:16
  • its working, but i have delay for 1-3 seconds, when i click to button. If i have small text, it works fast, but if i have very larege text, i have delay. –  Jun 25 '13 at 13:27
  • Ok, but have you fixed this with the AsyncTask? – Enrichman Jun 25 '13 at 13:39
  • No, everything is the same, i append 10000 symbols, and i think that setText() and append() works very slow with big text. So, I cant do it in background, i need to do it On Ui Thread :( –  Jun 25 '13 at 14:05

4 Answers4

0

How did you declare the thread ? Yu've to use something like that :

this.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            Log.e("TAG synchronize", "synchronize");
        }
});
marshallino16
  • 2,645
  • 15
  • 29
0

Use AsyncTask, they are meant for exactly what you want. They are a secondary thread that is easy to setup and executes from your main thread.

Use a loop inside the thread that appends portions of the generated text to the TextView using runOnUiThread(new Runnable(...)...) and call Thread.sleep(50) which forces the Thread to sleep for that much milliseconds to prevent the lag.

LuckyMe
  • 3,820
  • 2
  • 27
  • 35
  • If i do with AsyncTask i have error "Only the original thread that created a view hierarchy can touch its views." –  Jun 25 '13 at 08:08
  • Then you didn't do it fully, you didn't do the `runOnUiThread()` that makes it "run on the ui thread"! Only `appending` should be "run on the ui thread" don't put the entire `AsyncTask` operations to run on the ui as that would make it freeze as well. – LuckyMe Jun 25 '13 at 09:12
0

Use an AyncTask with something like:

private class MyTask extends AsyncTask<Void, Void, String> {

    private Context context;       

    public MyTask(Context context) {
        this.context = context;
    }

    public String doInBackground(Void... args) {
        // do your job here
        return myString;
    }

    public void onPostExecute(String myString) {
        // do your update here or you will get that error (the original thread one)
        ((TextView)context.findViewById(R.id.textview)).setText(myString);
    }
}

and then in your activity

new MyTask(MyActivity.this).execute();
Enrichman
  • 11,157
  • 11
  • 67
  • 101
0

First, remove the view, then add the text on background thread, then add the view back. Tell me if you need some sample code.

Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216