How to Get the Unique ID of an Android Device?
There are different types of unique IDs present for a single android device. We can get IDs such as a device ID, IMEI which is also a unique ID, and many others. In this article, we will take a look at How to Get the Unique ID of an Android Device.
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" > <!--displaying a simple text view--> < TextView android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_above = "@id/idTVUniqueID" android:layout_margin = "15dp" android:text = "Uniqie ID in Android" android:textAlignment = "center" android:textColor = "@color/black" android:textSize = "20sp" android:textStyle = "bold" /> <!--on below line creating text view for displaying id--> < TextView android:id = "@+id/idTVUniqueID" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_centerInParent = "true" android:gravity = "center" android:padding = "4dp" android:textAlignment = "center" android:textSize = "20sp" android:textStyle = "bold" /> </ RelativeLayout > |
Step 3: Working with the MainActivity file
Navigate to app > java > your app’s package name > MainActivity file and add the below code to it. Comments are added in the code to get to know in detail.
Kotlin
package com.gtappdevelopers.kotlingfgproject import android.os.Bundle import android.provider.Settings import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { // on below line we are creating variables. lateinit var uniqueIDTV: TextView override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // on below line we are initializing our variables. uniqueIDTV = findViewById(R.id.idTVUniqueID) // on below line we are getting device id. val android_device_id = Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID) // on below line we are setting text // as our id to our text view. uniqueIDTV.text = android_device_id } } |
Java
package com.gtappdevelopers.googlemapsroutes; import android.os.Bundle; import android.provider.Settings; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { // on below line we are creating variables. private TextView uniqueIDTV; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); // on below line we are initializing our variables. uniqueIDTV = findViewById(R.id.idTVUniqueID); // on below line we are creating a variable to get unique id String android_device_id = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID); // on below line setting id to our text view. uniqueIDTV.setText(android_device_id); } } |
Now run your application to see the output of it.
Output: