0

I'm a android newbie.

I need to create some kind of preloader for the Activity. Right now I click the button say "Show company" and afterwards I go to the next activity where the data is loaded from serwer and shown inside. The problem is that (from what I understand) the activity is connecting with internet and until connections is done nothing is show. User must wait and then suddelny (after few seconds - varies) he gets new 100% ready page based on new activity.

The best for me would be sth like: create a loding animation that is shown until the activity is fully loaded. (that would solve problem everywhere)

Alternative would be: loading the new activity before connecting with the internet url. When its loaded it say sth default like "Loading data" until full text is downloaded from url that will replace the first text.

Here is the code I use to load text from URL.

    try {
        // Create a URL for the desired page
        URL url = new URL(serwer_url);

        // Read all the text returned by the server
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String str;
        while ((str = in.readLine()) != null) {
            // str is one line of text; readLine() strips the newline character(s)
            Plain_str = Plain_str + str; 
        }
        Log.i("Plain read str", Plain_str);
        in.close();


    } catch (MalformedURLException e) {
    } catch (IOException e) {}      
    //end of reading file       

    //here is the anchor to the text in activity
    TextView MainText = (TextView)findViewById(R.id.TextMain);      
    MainText.setText(Html.fromHtml(Plain_str.toString()));
baron_bartek
  • 1,073
  • 2
  • 20
  • 39

1 Answers1

2

You can use an AsyncTask like this:

protected class Mytask extends AsyncTask<Void, Void, String>{

        @Override
        protected String doInBackground(Void... params) {
            String Plain_str= null;
            try {
                // Create a URL for the desired page
                URL url = new URL(serwer_url);

                // Read all the text returned by the server
                BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                String str;
                while ((str = in.readLine()) != null) {
                    // str is one line of text; readLine() strips the newline character(s)
                    Plain_str = Plain_str + str; 
                }
                Log.i("Plain read str", Plain_str);
                in.close();


            } catch (MalformedURLException e) {
            } catch (IOException e) {}   

            return Plain_str;
        }
        protected void onPostExecute(String str){
            TextView MainText = (TextView)findViewById(R.id.TextMain);      
            MainText.setText(Html.fromHtml(str.toString()));
        }
    }

And then to execute the Task

new MyTask().execute();
Gabriel Netto
  • 1,818
  • 1
  • 16
  • 26
  • Big Thanx! I have a problem hovewer. The doInBackground won't start for some reason. I put Log.i("Background works?", "Noooo :( "); but I dont get the message in the log - this is how I know. PS: I created my own code and had exacly the same problem. Any clues? – baron_bartek Nov 06 '12 at 13:42
  • 1
    you neew to create an instance of the class and call execute() on it – Gabriel Netto Nov 06 '12 at 13:56
  • Thanx. Now I see. It doesnt load automaticly. Ok, great. There is a bug in code from Gabriel. I fix them here: in onPostExecute it should be MainText.setText(Html.fromHtml(str.toString())); . Also this is how I used execute in order to make it work: Mytask task = new Mytask(); task.execute(); – baron_bartek Nov 06 '12 at 14:09