How to lock screen while playing games? I am wondering is there any app or a robust way of locking screen (front keys as well) while playing games in android phone. Is there any common app for locking/disabling the keys/touch when we play videos, youtube, playing games or reading ebook etc. 

The phone should go in lock mode as if by pressing lock button on side panel, and pressing it should prompt us to unlock the device. My LG L90 D410 has this built-in feature for playing local videos but not for other apps, games and youtube videos.
Okay, here's how to do it using Tasker:


Install Tasker
Create a new Task called lock screen, add an action to it as via 


Display &rarr; System Lock


Create a profile and add as context to it all games in the apps list it provides, or as you need it.
Link the lock screen task to this profile.


Your device should lock up now if you try to open any marked app.

Note: Tasker is a paid app. Also, the trigger system lock functionality needs you to allow Tasker as a device administrator.

Edit:

I figured out how to do this using MacroDroid, which is free for a limited feature set.


Install MacroDroid and open it.
Tap on create new macro
Select Application launched/closed from the trigger list.
Select application launched from the popup
Choose the required apps from the list.
Choose the action as screen on/off.
Choose screen off from the popup
Press the tick button.
On the constraints page, leave it at none and press the tick button.
Enter a relevant name for the macro and press OK in the resulting popup.


This worked for me. Again, this needs the app to be a device administrator, which will be indicated to you as you select the screen off action, for similar reasons as above.