[android-beginners] Re: Change text in textview dynamiclly

2009-09-08 Thread Justin Anderson
Sounds like a case where you are going to want to have separate threads...
one for the UI and one for establishing the socket connection.

The dev guide mentions when this should be done and I believe it also
addresses how to create multiple threads.

Thanks,
--
There are only 10 types of people in the world...
Those who know binary and those who don't.
--


On Tue, Sep 8, 2009 at 12:42 AM, Oskar  wrote:

>
> I would like to print text by default in a textview before I wait for
> a socket connection. When a connection is established I would like to
> receive messages from the client and print them to the screen.
>
> Hence I would like to first have one message printed and change this
> dynamiclly as time goes. The problem is that nothing is written to the
> screen until Oncreate() has ended.
>
> Please help me!
>
> Code:
> -
>
> package com.example.ComEx;
>
> import java.io.BufferedReader;
> import java.io.IOException;
> import java.io.InputStreamReader;
> import java.io.PrintWriter;
> import java.net.Socket;
> import java.net.UnknownHostException;
>
> import android.app.Activity;
> import android.os.Bundle;
> import android.widget.TextView;
>
> public class ComEx extends Activity {
>/** Called when the activity is first created. */
>private TextView t;
>@Override
>public void onCreate(Bundle savedInstanceState) {
>super.onCreate(savedInstanceState);
>setContentView(R.layout.main);
>
>
>
>Socket kkSocket=null;
>PrintWriter out;
>BufferedReader in;
>t = (TextView) findViewById(R.id.textTest);
>printIt("Test the client layout");
>
>try {
>kkSocket = new Socket("10.0.2.2", 5000);
>} catch (UnknownHostException e1) {
>// TODO Auto-generated catch block
>e1.printStackTrace();
>} catch (IOException e1) {
>// TODO Auto-generated catch block
>e1.printStackTrace();
>}
>
>try {
>out = new PrintWriter(kkSocket.getOutputStream(), true);
>in = new BufferedReader(new InputStreamReader(
>
>  kkSocket.getInputStream()));
>out.println("Hej Oskar");
>String fromServer;
>
>while ((fromServer = in.readLine()) != null) {
>
>printIt("Server "+fromServer);
>if (fromServer.equals("Bye."))
>break;
>
>}
>}catch (IOException e) {
>// TODO Auto-generated catch block
>e.printStackTrace();
>}
>
>
>}
>private void printIt(String s){
>t.setText(new StringBuffer(s));
>
>}
> }
>
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~--~~~~--~~--~--~---



[android-beginners] Re: Change text in textview dynamiclly

2009-09-08 Thread Oskar

How would multiple threads help us in this matter?

setText() does not seem to write out anything to display until the
currently running method has ended.

Anybody who has any example code?

Thanks!


On Sep 9, 6:06 am, Justin Anderson  wrote:
> Sounds like a case where you are going to want to have separate threads...
> one for the UI and one for establishing the socket connection.
>
> The dev guide mentions when this should be done and I believe it also
> addresses how to create multiple threads.
>
> Thanks,
> --
> There are only 10 types of people in the world...
> Those who know binary and those who don't.
> --
>
> On Tue, Sep 8, 2009 at 12:42 AM, Oskar  wrote:
>
> > I would like to print text by default in a textview before I wait for
> > a socket connection. When a connection is established I would like to
> > receive messages from the client and print them to the screen.
>
> > Hence I would like to first have one message printed and change this
> > dynamiclly as time goes. The problem is that nothing is written to the
> > screen until Oncreate() has ended.
>
> > Please help me!
>
> > Code:
> > -
>
> > package com.example.ComEx;
>
> > import java.io.BufferedReader;
> > import java.io.IOException;
> > import java.io.InputStreamReader;
> > import java.io.PrintWriter;
> > import java.net.Socket;
> > import java.net.UnknownHostException;
>
> > import android.app.Activity;
> > import android.os.Bundle;
> > import android.widget.TextView;
>
> > public class ComEx extends Activity {
> >    /** Called when the activity is first created. */
> >        private TextView t;
> >       �...@override
> >    public void onCreate(Bundle savedInstanceState) {
> >        super.onCreate(savedInstanceState);
> >        setContentView(R.layout.main);
>
> >        Socket kkSocket=null;
> >        PrintWriter out;
> >        BufferedReader in;
> >        t = (TextView) findViewById(R.id.textTest);
> >        printIt("Test the client layout");
>
> >        try {
> >                        kkSocket = new Socket("10.0.2.2", 5000);
> >                } catch (UnknownHostException e1) {
> >                        // TODO Auto-generated catch block
> >                        e1.printStackTrace();
> >                } catch (IOException e1) {
> >                        // TODO Auto-generated catch block
> >                        e1.printStackTrace();
> >                }
>
> >        try {
> >                out = new PrintWriter(kkSocket.getOutputStream(), true);
> >                        in = new BufferedReader(new InputStreamReader(
>
> >  kkSocket.getInputStream()));
> >                        out.println("Hej Oskar");
> >                        String fromServer;
>
> >                        while ((fromServer = in.readLine()) != null) {
>
> >                        printIt("Server "+fromServer);
> >                            if (fromServer.equals("Bye."))
> >                                break;
>
> >                        }
> >        }catch (IOException e) {
> >                        // TODO Auto-generated catch block
> >                        e.printStackTrace();
> >                }
>
> >    }
> >    private void printIt(String s){
> >        t.setText(new StringBuffer(s));
>
> >    }
> > }
>
>
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~--~~~~--~~--~--~---



[android-beginners] Re: Change text in textview dynamiclly

2009-09-09 Thread Justin Anderson
Have your onCreate do two things:
1) Set an initial value for the TextView
2) Spawn another thread to establish the connection

onCreate will then finish immediately after spawning the new thread,
allowing the initial value to be displayed.  You can then set up a Handler
for the thread to post back to and change the text displayed in the TextView
based on its current status.

This is described here:
http://developer.android.com/guide/practices/design/responsiveness.html

This document deals mostly with how to avoid the ANR dialog, but it seems to
me like this would be a situation where you would want to do something like
this as well.

Here are a few pointers the link above made:

If your application is doing work in the background in response to user
input, show that progress is being made
(ProgressBarand
ProgressDialogare
useful for this).

For games specifically, do calculations for moves in a child thread.

If your application has a time-consuming initial setup phase, consider
showing a splash screen or rendering the main view as quickly as possible
and filling in the information asynchronously. In either case, you should
indicate somehow that progress is being made, lest the user perceive that
the application is frozen.

Hope that helps,
Justin
--
There are only 10 types of people in the world...
Those who know binary and those who don't.
--


On Wed, Sep 9, 2009 at 12:48 AM, Oskar  wrote:

>
> How would multiple threads help us in this matter?
>
> setText() does not seem to write out anything to display until the
> currently running method has ended.
>
> Anybody who has any example code?
>
> Thanks!
>
>
> On Sep 9, 6:06 am, Justin Anderson  wrote:
> > Sounds like a case where you are going to want to have separate
> threads...
> > one for the UI and one for establishing the socket connection.
> >
> > The dev guide mentions when this should be done and I believe it also
> > addresses how to create multiple threads.
> >
> > Thanks,
> > --
> > There are only 10 types of people in the world...
> > Those who know binary and those who don't.
> > --
> >
> > On Tue, Sep 8, 2009 at 12:42 AM, Oskar  wrote:
> >
> > > I would like to print text by default in a textview before I wait for
> > > a socket connection. When a connection is established I would like to
> > > receive messages from the client and print them to the screen.
> >
> > > Hence I would like to first have one message printed and change this
> > > dynamiclly as time goes. The problem is that nothing is written to the
> > > screen until Oncreate() has ended.
> >
> > > Please help me!
> >
> > > Code:
> > > -
> >
> > > package com.example.ComEx;
> >
> > > import java.io.BufferedReader;
> > > import java.io.IOException;
> > > import java.io.InputStreamReader;
> > > import java.io.PrintWriter;
> > > import java.net.Socket;
> > > import java.net.UnknownHostException;
> >
> > > import android.app.Activity;
> > > import android.os.Bundle;
> > > import android.widget.TextView;
> >
> > > public class ComEx extends Activity {
> > >/** Called when the activity is first created. */
> > >private TextView t;
> > >@Override
> > >public void onCreate(Bundle savedInstanceState) {
> > >super.onCreate(savedInstanceState);
> > >setContentView(R.layout.main);
> >
> > >Socket kkSocket=null;
> > >PrintWriter out;
> > >BufferedReader in;
> > >t = (TextView) findViewById(R.id.textTest);
> > >printIt("Test the client layout");
> >
> > >try {
> > >kkSocket = new Socket("10.0.2.2", 5000);
> > >} catch (UnknownHostException e1) {
> > >// TODO Auto-generated catch block
> > >e1.printStackTrace();
> > >} catch (IOException e1) {
> > >// TODO Auto-generated catch block
> > >e1.printStackTrace();
> > >}
> >
> > >try {
> > >out = new PrintWriter(kkSocket.getOutputStream(), true);
> > >in = new BufferedReader(new InputStreamReader(
> >
> > >  kkSocket.getInputStream()));
> > >out.println("Hej Oskar");
> > >String fromServer;
> >
> > >while ((fromServer = in.readLine()) != null) {
> >
> > >printIt("Server "+fromServer);
> > >if (fromServer.equals("Bye."))
> > >break;
> >
> > >}
> > >