Android Toasts receives touch events on some phone, not on others

I am using Toast with a custom view. I instantiate the view and call setView on the toast. The toast was supposed to pop up and not get focus and touch events, and it worked well. After the application was launched, users complained that on several phone models, such as the Galaxy Note, Toast received touch events, and the application below did not.

I printed the params layout flags (WindowManager.LayoutParams) that opens in the setLayoutParams method. It turns out that on most devices the value is 0x000098, but on some it is 0x040088. On devices that Toast receives touch events, the FLAG_NOT_TOUCHABLE flag is removed and the FLAG_WATCH_OUTSIDE_TOUCH flag is added. This explains why the toast receives sensory events.

But what makes this difference? Is there a way to make the toast insensitive?

+7
source share
1 answer

I have a job. You must have access to the view that covers the toast. You can basically capture touch events going to Toast. Then change the MotionEvent X and Y. Then send the MotionEvent to the hidden view. It seems a bit hacked to me, but it works. I would have thought it would be better to change the flags on the toast, but I cannot find them.

In action, a Toast is created:

final Taost toast = Toast.makeText(this, "Text", Toast.LENGTH_LONG); final View myCoveredView = findViewById(R.id.my_covered_view); final View toastView = toast.getView(); toastView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(final View v2, final MotionEvent event) { //Make sure the view is accessible if(myCoveredView != null){ //Set the touch location to the absolute screen location event.setLocation(event.getRawX(), event.getRawY()); //Send the touch event to the view return myCoveredView.onTouchEvent(event); } return false; } }); 

If anyone has a better way, I would love to be here.

+1
source

All Articles