How to display a Toast inside a Handler/thread?

android, toast

Solution

put

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

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

after this line

  Log.d("ClientActivity", "C: Connecting...");

Problem

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; } } } ``` }

Original source