How to Get the Unique ID of an Android Device?
Last Updated :
31 Aug, 2022
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
android:id = "@+id/idRLContainer"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical"
tools:context = ".MainActivity" >
< 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" />
< 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() {
lateinit var uniqueIDTV: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
uniqueIDTV = findViewById(R.id.idTVUniqueID)
val android_device_id =
Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
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 {
private TextView uniqueIDTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uniqueIDTV = findViewById(R.id.idTVUniqueID);
String android_device_id = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
uniqueIDTV.setText(android_device_id);
}
}
|
Now run your application to see the output of it.
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...