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 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: