-2

I've been trying to use Google to search on how to make a simple quit button in java on Android Studio IDE. Unfortunately all of the tutorials on Youtube or otherwise are all tutorials on how to make functioning buttons in the Kotlin programming language. The reason why I need to make the button in Java is that it is for College. I am new to the software developing world so any help would be appreciated.

  • 2
    Does this answer your question? [How to exit an Android app programmatically?](https://stackoverflow.com/questions/17719634/how-to-exit-an-android-app-programmatically) – m0skit0 Jul 15 '22 at 20:27

1 Answers1

0

Create a button with the following code in your .xml file:

<Button
    android:text="Quit"
    android:id="@+id/quit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

In your Activity, get the button view in onCreate or where you need it and add an onClicklistener:

Button quit = findViewById(R.id.quit);

quit.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {

        finishAndRemoveTask();    

    }

});

This works for API 21 and above.

John T
  • 814
  • 10
  • 17