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);
}
}