Skip to content
Related Articles

Related Articles

How to Fetch Device ID in Android Programmatically?

Improve Article
Save Article
Like Article
  • Last Updated : 12 Jan, 2022

Android Device ID is a unique code, string combinations of alphabets and numbers, given to every manufactured android device. This code is used to identify and track each android device present in the world. In Android, the Device ID is typically related to the Google Play Services and is most commonly used in ad personalization. These IDs are collected and are used for displaying particular types of ads. This type is calculated upon user search and navigation tracking. One can root the device to erase the Device ID and avoid tracking and ad personalization. In this article, we will show you how you could fetch the Device ID of your Android device. Follow the below steps once the IDE is ready.

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. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 


<?xml version="1.0" encoding="utf-8"?>
    <!-- This TextView will display the Device ID -->
        app:layout_constraintTop_toTopOf="parent" />

Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.


package org.geeksforgeeks.deviceid
import android.os.Bundle
import android.provider.Settings
import android.widget.TextView
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Declaring and initializing a constant for 
        // the TextView from the layout file (activity_main.xml)
        val mTextView1 = findViewById<TextView>(
        // Fetching Android ID and storing it into a constant
        val mId = Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
        // Displaying the Android ID into the TextView
        mTextView1.text = mId


You would see that when the app launches, the device ID is fetched and is displayed in the TextView.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!