0

I'm trying to create a small floating view which is displayed after some user interaction. This view is displaying 2 buttons. I have a problem with attaching the click event to these buttons and whole layout.

In short: Overlay window is displayed but buttons inside are not working.

I tried to use bringToFront method after the addView but without luck.

How can I do in the right way, please?

Many thanks for any advice.

Code snippet:

@Override
    public void onCreate() {
        try {
            super.onCreate();
            Logger.d("onCreate");
            mContext = this;
            resultServiceIntent = new Intent(this, ResultService.class);
            windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mParentLayout = (LinearLayout) mInflater.inflate(R.layout.result_layout,
                    null);
            textViewSourceText = (TextView) mParentLayout.findViewById(R.id.textViewSourceText);
            textViewTranslatedText = (TextView) mParentLayout.findViewById(R.id.textViewTranslatedText);
            WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                    WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                    PixelFormat.TRANSLUCENT);

            params.gravity = Gravity.TOP | Gravity.CENTER;
            //params.x = 0;
            //params.y = 100;

            // SET VALUES TO TEXT VIEWS
            textViewSourceText.setText(Translation.getInstance().getSourceText());
            textViewTranslatedText.setText(Translation.getInstance().getTranslatedText());

            mParentLayout.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    Logger.d("onTouch");
                    return true;
                }
            });

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

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Logger.d("Clicked close button");
                    //windowManager.removeView(mParentLayout);
                    stopService(resultServiceIntent);
                }
            });

            Button copyTranslation = (Button) mParentLayout.findViewById(R.id.copyTranslationButton);
            copyTranslation.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Logger.d("Clicked on the copy translation layout");
                    stopService(resultServiceIntent);

                    //TODO: move to separated method
                    ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                    ClipData clip = ClipData.newPlainText("dwd", Translation.getInstance().getTranslatedText());
                    clipboard.setPrimaryClip(clip);
                    ToastHelper.showToastMessage(mContext,
                            mContext.getResources().getString(R.string.translation_copied_to_clipboard), false);

                }
            });
            windowManager.addView(mParentLayout, params);
            mParentLayout.bringToFront();

        } catch (Exception e) {
            Logger.e(e.getMessage());
            ToastHelper.showToastMessage(mContext,
                    mContext.getResources().getString(R.string.error_during_displaying_result),
                    Constants.Global.DEBUG_ENABLED);
            TrackingEventLogHelper.logException(e, Constants.Global.EXCEPTION,
                    mContext.getResources().getString(R.string.error_during_displaying_result), true);
        }
    }
Pushpendra
  • 2,791
  • 4
  • 26
  • 49
redrom
  • 11,502
  • 31
  • 157
  • 264
  • Have you checked [this](https://stackoverflow.com/q/14938853/1083957) answer? – azizbekian May 24 '17 at 11:30
  • Thanks Now I'm able to execute onCLick events. But not able to get focus on view under the top view. For example, when the top view is displayed in the browser, I'm not able to get focus in the browser when I touching outside the top view. – redrom May 24 '17 at 12:14

0 Answers0