How to Fetch Device ID in Android Programmatically? Last Updated : 30 Aug, 2022 Comments Improve Suggest changes Like Article Like Report The 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 ImplementationStep 1: Create a New Project in Android StudioTo create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android. Step 2: Working with the XML FilesNext, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail. XML <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <!-- This TextView will display the Device ID --> <TextView android:id="@+id/textview_1" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> Step 3: Working with the MainActivity FileGo to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail. Kotlin import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.provider.Settings import android.widget.TextView class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Declaring and initializing a constant for // the TextView from the layout // file (activity_main.xml) val mTextView1 = findViewById<TextView>(R.id.textview_1) // 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 } } Java import android.os.Bundle; import android.provider.Settings; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Declaring and initializing a constant for the TextView from the layout file (activity_main.xml) TextView mTextView1 = findViewById(R.id.textview_1); // Fetching Android ID and storing it into a constant String mId = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID); // Displaying the Android ID into the TextView mTextView1.setText(mId); } } Output:You would see that when the app launches, the device ID is fetched and displayed in TextView. Comment More infoAdvertise with us Next Article How to Fetch Device ID in Android Programmatically? aashaypawar Follow Improve Article Tags : Java Kotlin Android Practice Tags : Java Similar Reads Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to 12 min read Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca 7 min read Like