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 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. The code for that has been given in both Java and Kotlin Programming Language for Android.
Step 2: Working with the XML Files
Next, 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 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 File
Go 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.