2

I am making an app which is reading the text file and display content into EditText, but I am facing one issue is that when the file is too large (which is more than 1mb) than setting text into EditText takes some time and app get stuck.

If anyone knows that is there any other option to display text into EditText then please help me. I got stuck over here

edittext.setText(text);//got stuck here

this readFile() is inside AsyncTask and setText() is inside onPostExecute()

public String readFile(String fileName) {
    StringBuilder builder = null;
    BufferedReader reader = null;
    try {
        File file = new File(fileName);
        if (!file.exists()) {
            throw new RuntimeException("File not found");
        }


        reader = new BufferedReader(new FileReader(file));
        builder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line);
            builder.append("\n");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return builder != null ? builder.toString() : "";
}

Edit

Now I am not using BufferedReader to read a file. I am reading a file using c code

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Priyanka
  • 3,369
  • 1
  • 10
  • 33
  • check this: http://www.java2s.com/Code/Android/UI/NoteEditor.htm – feridok Dec 17 '19 at 08:26
  • I checked this, but this is not useful for me, Thanks – Priyanka Dec 17 '19 at 08:28
  • what is your `text` look like? – Donald Wu Dec 17 '19 at 08:36
  • 1
    [May be relevant - link.](https://stackoverflow.com/questions/29801232/edittext-performance-understanding-gpu-process-time-on-profile-gpu-rendering). I had a similar issue writing a text editing application once. – PPartisan Dec 17 '19 at 08:36
  • the `text` is a `string` which is read from `.txt` file – Priyanka Dec 17 '19 at 08:43
  • 1
    This answer is a very interesting solution to the problem: https://stackoverflow.com/a/41904130/1219389 Essentially, you create your `EditText` on a background thread and set the text there, but don't add it to your layout until `onPostExecute`. – PPartisan Dec 17 '19 at 08:49
  • @PPartisan Wouldn't this still cause a delay? I might be wrong but the only difference I forsee is the visibility of the EditText. The delay would still be there. – Taslim Oseni Dec 17 '19 at 08:55
  • I expect the delay would still be there, but it wouldn't block the UI thread so you could show some kind of feedback at least, like a loading dialog – PPartisan Dec 17 '19 at 08:58
  • @PPartisan, I have changed the reading file code. Now I am reading file form `c`, so the reading file is faster than `BufferedReader` which is less than 1 sec, so now the delay is while set text into `EditText` – Priyanka Dec 17 '19 at 09:35
  • Yes, I understand, and I never found a good solution to that. Calling `setText()` on an `EditText` that is created in the background at least means you can show some kind of visual feedback that the text is being loaded - otherwise any loading feedback you show will be blocked. – PPartisan Dec 17 '19 at 09:40
  • @PPartisan, how can I show any loader over there, because I will not get any event that all text is set in the `EditText`, is there any callback in which we can get such event? – Priyanka Dec 17 '19 at 09:43
  • No, you set the text in `doInBackground()`, which blocks that thread. When it is complete your `AsyncTask` will call `onPostExectue`. Look at the answer I linked - it will be much faster than trying to explain it to you. – PPartisan Dec 17 '19 at 09:46
  • okay, let me implement like that – Priyanka Dec 17 '19 at 09:47
  • @PPartisan an answer you suggested me will not work because we can not access any of widget on the inferred thread – Priyanka Dec 17 '19 at 10:08
  • Make sure you follow the answer carefully. Attempting to call `setText()` from a background thread will cause an error _unless_ that view is newly created and hasn't been attached the layout yet. – PPartisan Dec 17 '19 at 10:20

1 Answers1

0

This behaviour is expected. Intuitively, it would take more time to read larger files. There are a couple of options you can use to manage this situation.


Scenario one:

Do you have access to the file long before you have to perform edittext.setText(...)?

If yes, you can use a service to get the text you need to display long before hand and save it to a more accessible storage (i.e SharedPreferences). Displaying the text after this would result to zero delay.

Scenario two:

If scenario one is not fulfilled...

There is actually nothing wrong with a delay. What matters is how you present the delay to your user. You can make use of a progressBar or a loading animation to abstract this delay.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69