Question: I am new to Xmpp and am confused right now.When I add a new Toast to the ConnectionListener interface methods the Toast does not show up in the application.

My code:

```
  conn2.addConnectionListener(new ConnectionListener()
    {
        @Override
        public void connected(XMPPConnection xmppConnection)
        {
            Toast.makeText(getApplicationContext(), "connected", Toast.LENGTH_LONG).show();
        }

        @Override
        public void authenticated(XMPPConnection xmppConnection, boolean b)
        {
            Toast.makeText(getApplicationContext(), "authenticated", Toast.LENGTH_LONG).show();
        }

        @Override
        public void connectionClosed()
        {
            Toast.makeText(getApplicationContext(), "conclose", Toast.LENGTH_LONG).show();
        }

        @Override
        public void connectionClosedOnError(Exception e)
        {
            Toast.makeText(getApplicationContext(), "conclose", Toast.LENGTH_LONG).show();
        }

        @Override
        public void reconnectionSuccessful()
        {
            Toast.makeText(getApplicationContext(), "reconnect", Toast.LENGTH_LONG).show();
        }

        @Override
        public void reconnectingIn(int i)
        {
            Toast.makeText(getApplicationContext(), "reconnect", Toast.LENGTH_LONG).show();
        }

        @Override
        public void reconnectionFailed(Exception e)
        {

        }
    });

```

This is what I expect the code to do:Whenever a connection is established there should be a new Toast that appears on the screen saying "Connected".What am I doing wrong.Is it the wrong way to do this?

Answer: 
You have to use a handler but should look this way: 

`new Handler(Looper.getMainLooper())`
You will need to create a handler in order to show the toast message.

For example:

```
    private Handler mHandler = new Handler();

```

this creates the handler object. After that at any of your connection listener code, add this:

For example, i'm displaying the toast at reconnectionSuccessful() method :

```
    @Override
    public void reconnectionSuccessful() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(this, "Yay, Reconnect Success!",Toast.LENGTH_LONG).show();
            }
        });
    }

```