How to Quit Android Application Programmatically? Last Updated : 17 Jul, 2022 Comments Improve Suggest changes Like Article Like Report When we want to implement an exit AlertDialog in our android application we have to programmatically exit our android application. In this article, we will take a look at How to Quit the Android application programmatically. We will be adding a button and on clicking on that button we will be closing our application. A sample video is given below to get an idea about what we are going to do in this article. Note: This Android article covered in both Java and Kotlin languages. Step by Step Implementation Step 1: Create a New Project in Android Studio To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Step 2: Working with the activity_main.xml file Navigate to app > res > layout > activity_main.xml and add the below code to it. Comments are added in the code to get to know in detail. XML <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/idRLContainer" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <!--on below line we are creating a text for our app--> <TextView android:id="@+id/idTVHeading" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@id/idBtnCloseApplication" android:layout_centerInParent="true" android:layout_margin="20dp" android:gravity="center" android:padding="10dp" android:text="Click the button to close the application" android:textAlignment="center" android:textColor="@color/black" android:textSize="20sp" android:textStyle="bold" /> <!--on below line we are creating a button--> <Button android:id="@+id/idBtnCloseApplication" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="20dp" android:text="Close the application" android:textAllCaps="false" /> </RelativeLayout> Step 3: Working with the MainActivity file Navigate to app > java > your app's package name > MainActivity file and add the code below. Comments are added in the code to get to know in detail. Kotlin package com.gtappdevelopers.kotlingfgproject import android.os.Bundle import android.widget.Button import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { // on below line we are creating a variable. lateinit var closeApplicationBtn: Button override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // on below line we are creating and // initializing variable for activity val activity: MainActivity = MainActivity() // on below line we are initializing our variables. closeApplicationBtn = findViewById(R.id.idBtnCloseApplication) // on below line we are adding click listener for our button closeApplicationBtn.setOnClickListener { // on below line we are finishing activity. activity.finish() // on below line we are exiting our activity System.exit(0) } } } Java package com.gtappdevelopers.kotlingfgproject; import android.os.Bundle; import android.view.View; import android.widget.Button; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { // on the below line we are creating a variable. private Button closeApplicationBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // on below line we are initializing variables with ids. closeApplicationBtn = findViewById(R.id.idBtnCloseApplication); // on below line we are adding click listener for our button closeApplicationBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // on below line we are finishing activity. MainActivity.this.finish(); // on below line we are exiting our activity System.exit(0); } }); } } Now run your application to see the output of it. Output: Comment More infoAdvertise with us Next Article How to Quit Android Application Programmatically? C chaitanyamunje Follow Improve Article Tags : Java Kotlin Android Practice Tags : Java Similar Reads How to Uninstall Android Apps Programmatically? In this article, we are going to see how we can programmatically uninstall any Android device from your Android device programmatically by specifying the App name. A sample video is given below to get an idea about what we are going to do in this article. Step-by-Step ImplementationStep 1: Create a 3 min read How to Change ActionBar Title Programmatically in Android? Whenever we develop an Android application and run it, we often observe that the application comes with an ActionBar with the name of the application in it. This happens by default unless explicitly changed. This text is called the title in the application. One can change the title of the applicatio 3 min read How to programmatically hide Android soft keyboard In this article, we will learn about how to hide soft keyboard programmatically. The keyboard generally hides but there are certain instances when it does not hide. So for better user experience, the keyboard is hidden programmatically. Without using below approach, the default response of the app i 2 min read How to Change App Icon of Android Programmatically in Android? In this article, we are going to learn how to change the App Icon of an App on the Button Click. This feature can be used when we have an app for different types of users. Then as per the user type, we can change the App Icon Dynamically. Step by Step Implementation Step 1: Create a New Project To 3 min read How to Close Alert Dialog Box in Android Programmatically? AlertDialog is a flash or an alert message with options that let the user proceed or deny any process or action. AlertDialog generally consists of the main title, the message, and two buttons, technically termed as a positive button and a negative button. Both positive and negative buttons can be pr 2 min read How to Fetch Currently Running Tasks in Android Programmatically? Android devices run several tasks in the background for providing a seamless experience to the users. Importantly, tracking background and foreground usages is important as it directly impacts the battery life and static memory, i.e. RAM. One can enable the developer option in the settings to track 2 min read How to Check the Battery Level in Android Programmatically? Sometimes, it is useful to determine the current battery level. One may choose to reduce the rate of your background updates if the battery charge is below a certain level. But one cannot continuously monitor the battery state. In general, the impact of constantly monitoring the battery level has a 2 min read How to Check GPS is On or Off in Android Programmatically? GPS (Global Positioning System) is a satellite-based navigation system that accommodates radio signals between satellite and device to process the device's location in the form of coordinates. GPS gives latitude and longitude values of the device. Recent mobile phones are equipped with GPS modules t 2 min read How to Programmatically Enable/Disable Bluetooth in Android? In Android Phone, it is very much easy to enable/disable Bluetooth by using the Bluetooth icon, but have you wondered how to do this task programmatically in Android. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this pr 3 min read Different Ways to Programmatically Restart an Android App on Button Click Sometimes we want to refresh an Activity, but there it is not possible to refresh. In that place, if we restart our app then it automatically gets refreshed. Also, we can use this to restart our app whenever it crashes. Most of the time when we open any app then it fetches all the data currently ava 3 min read Like