Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a Toast inside a Handler/thread?

Tags:

android

toast

I want to display a toast once the message is sent to a socket.After this "Log.d("ClientActivity", "C: Sent.");"

Whether I need to create a separate thread to display Toast?

public class ClientActivity extends Activity {
private Handler handler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.client);
    serverIp = (EditText) findViewById(R.id.EditText01);
    message =(EditText) findViewById(R.id.EditText02);
    connectPhones = (Button) findViewById(R.id.Button01);

}

    public void Click1(View v) {
        if (!connected) {
            serverIpAddress = serverIp.getText().toString();
            if (!serverIpAddress.equals("")) {
                Thread cThread = new Thread(new ClientThread());
                cThread.start();
            }
        }
    }


private class ClientThread implements Runnable {

    public void run() {
        try {
            InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
            Log.d("ClientActivity", "C: Connecting...");
            Socket socket = new Socket(serverAddr, ServerActivity.SERVERPORT);
            connected = true;
            while (connected) {
                try {
                    if(i>5)
                    {


                    } 
                    else
                    {   
                        Log.d("ClientActivity", "C: Sending command.");
                        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
                                .getOutputStream())), true);
                        // where you issue the commands
                        message1= message.getText().toString();
                        out.println(message1);
                        i=i+1;
                        Log.d("ClientActivity", "C: Sent.");
                    }    
                } catch (Exception e) {
                    Log.e("ClientActivity", "S: Error", e);
                }
            }
            socket.close();
            Log.d("ClientActivity", "C: Closed.");
        } catch (Exception e) {
            Log.e("ClientActivity", "C: Error", e);
            connected = false;
        }
    }
}

}

like image 868
Shan Avatar asked May 16 '12 01:05

Shan


People also ask

Where do you put the toast message?

Positioning your Toast A standard toast notification appears near the bottom of the screen, centered horizontally. You can change this position with the setGravity(int, int, int) method. This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.

Which method is used to print the toast message?

Display the created Toast Message using the show() method of the Toast class. The code to show the Toast message: Toast. makeText(getApplicationContext(), "This a toast message", Toast.


2 Answers

put

  runOnUiThread(new Runnable() {
                 public void run() {

                     Toast.makeText(ClientActivity.this,"asdf",Toast.LENGTH_LONG).show();
                }
            });

after this line

  Log.d("ClientActivity", "C: Connecting...");
like image 153
5hssba Avatar answered Oct 13 '22 22:10

5hssba


You cannot create a toast from inside a thread. Because this new thread does not have access to the getApplicationContext() that you pass on to it. You somehow have to establesh a communication with the UI thread(i.e the main thread).
So whenever you want to toast something do it in the handler.Post(Runnable) method. Handler is the middle man between the UI thread and the separate thread that you are running. All UI Operations will have to be done in handler.Post(Runnable)'s run() method.

So in your activity to display a toast do this :

private class ClientThread implements Runnable {

    public void run() {
        try {
            InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
             .....
             .....
              message1= message.getText().toString();
                        out.println(message1);
                        i=i+1;
                        Log.d("ClientActivity", "C: Sent.");
                        handler.post(new Runnable(){
                          public void run()
                          {
                             Toast.make(....);
                           }
                         });

Don't forget to declare and initialize a handler object in your main activity(outside the thread)

handler=new Handler();
like image 3
Ashwin Avatar answered Oct 13 '22 20:10

Ashwin