Anderson Spencer
Anderson Spencer

Reputation: 77

Make screen not clickable until a button is clicked

I'm working on a kotlin app, My activity is in fullscreen and I want to make a button in which the screen can't be exit or back or anything unless the button is clicked. Like the one in the video player lock type.

Upvotes: 3

Views: 572

Answers (1)

iknow
iknow

Reputation: 9892

My first idea was to make boolean isLocked and after clicking button change this value to the opposite and override all functions/events etc which You would like to lock and if isLocked is true just don't execute them.
Here is a simple code where I locked back button (in java but it can be easily changed to Kotlin)

public class MainActivity extends AppCompatActivity
{
    boolean isLocked = false;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                isLocked ^= true;
                System.out.println(isLocked);
            }
        });
    }

    @Override
    public void onBackPressed()
    {
        if (!isLocked)
        {
            super.onBackPressed();
        }
    }
}

I think that in a similar way You can lock every event.

You can also make something like this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if (!isLocked)
    {
        return super.onKeyDown(keyCode, event);
    }
    else
    {
        return true;
    }
}

This will cancel every event like clicking back button or changing volume

Upvotes: 1

Related Questions