0

so I'm coding an app in Android Studio with Java right now for my company (I'm working in a daycare for school children) in which the kids are able to check themselves in. (Coming from school to us, click on their name and then its in a list)

At one activity there is a lock button at the top, so that if it is pressed the user is forbidden to exit the app or return to the previous activity without entering a password. So kids can't mess with it.

I've already searched some time in the existing threads but I've only found one solution that came close and since I'm not really a coder I don't know very much about it, just the basics.

What I've already tried:

                            public boolean onKeyDown(int keyCode, KeyEvent event) {
                            if (keyCode == KeyEvent.KEYCODE_HOME) {
                                Log.i("TEST", "Home Button");  // here you'll have to do something to prevent the button to go to the home screen
                                return true;
                            }
                            return super.onKeyDown(keyCode, event);

and

                        public void onBackPressed(){
                            Toast.makeText(getApplicationContext(),"You Are Not Allowed to Exit the App", Toast.LENGTH_SHORT).show();
                        }

I know I haven't changed the first one because I didn't know what to do and I can't find anything for it.

I've made an AlertDialog for the Button for safety. Some of my colleagues aren't too familiar with electronic devices and need everything foolproof.

This is what I've coded for the AlterDialog so far:

      eGeoeffnet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder myAlertBuilder2 = new AlertDialog.Builder(Anmeldung.this);
                myAlertBuilder2.setTitle("Navigation sperren");
                myAlertBuilder2.setMessage("Möchtest du die Navigation sperren?");
                myAlertBuilder2.setPositiveButton("Ja", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                myAlertBuilder2.setNegativeButton("Nein", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(Anmeldung.this, "Du hast 'Nein' ausgewählt!", Toast.LENGTH_SHORT).show();
                    }
                });
                myAlertBuilder2.show();
            }
        });

Could someone tell me what I need to put into the positiveButton area?

Translation for the german parts:

"Navigation sperren" -> Lock navigation

"Möchtest du die Navigation sperren" -> Do you want to lock the navigations?

"Ja" -> Yes

"Nein" -> No

"Du hast 'Nein' ausgewählt!" -> You've chosen 'No'!

"Anmeldung" -> Log In

"Geoeffnet" -> Opened

I hope my thread doesn't sound too stupid, as I said earlier, I'm no real coder, it's just a hobby of mine.

Thanks in advance for your time!

Zain
  • 37,492
  • 7
  • 60
  • 84
Veronique
  • 3
  • 3
  • What device is being used here? What other buttons are used in this app? You can override all other buttons so that when the buttons need to be locked nothing happens. – Darcia14 Jun 19 '21 at 15:41
  • It is supposed to run on a samsung tablet – Veronique Jun 19 '21 at 19:58
  • and i think the main focus should be on the home button, that should be disabled. If i remember it correctly the tablet doesn't show that back button at the bottom.. – Veronique Jun 19 '21 at 20:17
  • Do have a look at this thread https://stackoverflow.com/questions/2162182/android-is-it-possible-to-disable-the-click-of-home-button – Darcia14 Jun 20 '21 at 07:48
  • I can't understand what they're talking about, it's too much. I can't figure out which part I have to put where.. – Veronique Jun 20 '21 at 15:00

1 Answers1

0

As explained in this post: Android - Is It possible to disable the click of home button. You can set you app Activity to be an Home Launcher activity. That causes the App to be opened (stay open) when the Home button is pressed.

Add this to your manifest.xml file on the activity you want to keep open/open with the home button.


<activity
            android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" /> // add these
                <category android:name="android.intent.category.LAUNCHER" /> // add these
                <category android:name="android.intent.category.HOME" /> // add these
            </intent-filter>
</activity>

Darcia14
  • 214
  • 1
  • 13
  • Thanks! But is it possible to only use it when a button was clicked? and to disable it again? because the tablet we use is used after our shifts for different apps. – Veronique Jun 20 '21 at 17:14
  • And I need that function for 2 activities. The Login Area for kids AND the Logout Area for kids.. – Veronique Jun 20 '21 at 17:23