title: Chat head Click events
body: <p>I used an online tutorial to create a simple application which adds ImageButton to the window. By window I mean that the ImageButton is displayed above the rest of the activities. It is always visible. </p>
  
  <p>I don't know how to handle click events for the chat head. I set a onClickListener but it doesn't do anything.</p>
  
  <p>Here's my ChatHeadService.java</p>
  
  <pre><code>package com.example.test;
  
  import android.app.Service;
  import android.content.Intent;
  import android.graphics.PixelFormat;
  import android.os.IBinder;
  import android.view.Gravity;
  import android.view.MotionEvent;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.WindowManager;
  import android.widget.ImageButton;
  import android.widget.ImageView;
  
  public class ChatHeadService extends Service {
  
      private WindowManager windowManager;
      private ImageView chatHead;
  
      @Override
      public IBinder onBind(Intent intent) {
          // Not used
          return null;
      }
  
      @Override
      public void onCreate() {
          super.onCreate();
  
          windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
  
          chatHead = new ImageButton(this);
          chatHead.setImageResource(R.drawable.ic_launcher);
  
          final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                  WindowManager.LayoutParams.WRAP_CONTENT,
                  WindowManager.LayoutParams.WRAP_CONTENT,
                  WindowManager.LayoutParams.TYPE_PHONE,
                  WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                  PixelFormat.TRANSLUCENT);
  
          params.gravity = Gravity.CENTER;
          params.x = 0;
          params.y = 100;
  
          windowManager.addView(chatHead, params);
          chatHead.setClickable(true);
          chatHead.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View arg0) {
  
  
              }
          });
  
          chatHead.setOnTouchListener(new View.OnTouchListener() {
              private int initialX;
              private int initialY;
              private float initialTouchX;
              private float initialTouchY;
  
              @Override
              public boolean onTouch(View v, MotionEvent event) {
                  switch (event.getAction()) {
                  case MotionEvent.ACTION_DOWN:
                      initialX = params.x;
                      initialY = params.y;
                      initialTouchX = event.getRawX();
                      initialTouchY = event.getRawY();
                      return true;
                  case MotionEvent.ACTION_UP:
                      return true;
                  case MotionEvent.ACTION_MOVE:
                      params.x = initialX
                              + (int) (event.getRawX() - initialTouchX);
                      params.y = initialY
                              + (int) (event.getRawY() - initialTouchY);
                      windowManager.updateViewLayout(chatHead, params);
                      return true;
                  }
                  return false;
              }
          });
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          if (chatHead != null)
              windowManager.removeView(chatHead);
      }
  }
  </code></pre>