Tarun Talreja Tarun Talreja - 1 year ago 81
Android Question

Why I am not able to use imageview in an activity which extends BroadcastReceiver?

Below is my ChatHeadService class and I am creating a new imageview in the oncreate() method and i can see the imageview.I am using it in the way similar to chat heads and my OnTouchListener is working but onClickListener is not working i want to open a new activity on clicking the imageview but it is not working even the toast doesn't appear.Thanks for any help in advance.

package com.tarun.notifyme2;

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

public class ChatHeadService extends Service {

private WindowManager windowManager;
private ImageView chatHead;
WindowManager.LayoutParams params;
public int onStartCommand(Intent intent, int flags, int startId) {
int res = super.onStartCommand(intent, flags, startId);
return res;
public void onCreate() {

windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

chatHead = new ImageView(this);
params= new WindowManager.LayoutParams(

params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 200;
chatHead.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"You clicked the imageview",Toast.LENGTH_LONG).show();
Log.i("tag","You clicked the imageview");
Intent i = new Intent(view.getContext(),SendNoti.class);

//this code is for dragging the chat head
chatHead.setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;

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;
windowManager.addView(chatHead, params);

public void onDestroy() {
if (chatHead != null)

public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;

Answer Source

you are "swallowing" your touch events inside onTouch(View v, MotionEvent event) by returning true (means you are done all you needed and NOT notify onClick, onLongClick etc.). you may "emulate" onClick by setting some flag in ACTION_MOVE and check it in ACTION_UP if last "touch action" was moving or just down/up click (then return false or just write your code without any onClick set)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download