0

In service for displaying custom view over all windows I need to set onClick listener.

@Override public void onCreate() {
        super.onCreate();

        mContext = this;

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout parent = (LinearLayout) inflater.inflate(R.layout.result_layout,
                null);

        // Here i need to add onClick listener    

        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.TOP | Gravity.LEFT;
        params.x = 0;
        params.y = 100;

        windowManager.addView(parent, params);
    }

I tried to create button lister from inflater but without luck. What is the right approach to set click event listener for selected button by button ID?

Many thanks for any advice.

redrom
  • 11,502
  • 31
  • 157
  • 264

2 Answers2

0

Resolved using the following code which is works in the service well:

@Override public void onCreate() {
    super.onCreate();

    mContext = this;


    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mParentLayout = (LinearLayout) inflater.inflate(R.layout.result_layout,
            null);

    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.TOP | Gravity.LEFT;
    params.x = 0;
    params.y = 100;

    windowManager.addView(mParentLayout, params);


    Button b = (Button) mParentLayout.findViewById(R.id.closeButton);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            windowManager.removeView(mParentLayout);
        }
    });

}

Inspired from:

Could not get Touch event for TYPE_SYSTEM_OVERLAY

Community
  • 1
  • 1
redrom
  • 11,502
  • 31
  • 157
  • 264
-1

Android can play tricks on what you think might work, but doesn't.

First, What I typically do is from onCreate() I place a call to: InitializePage(); where I then put all my initialize code. It's just my style.

Then in my InitializePage() function I initialize the button, then enter the OnClickListener. I use Android Studio with Intellisense so it writes the code for me....

       mSendButton = (TextView)findViewById(R.id.send_button1);
       mSendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) 
        {..... 
           });

It should be that easy. Good luck

IrvineCAGuy
  • 211
  • 2
  • 12