How to find Android ID in Android Kotlin or Java.

Harshita Bambure
2 min readJan 12, 2022

--

Nowadays due to security reasons, we are not able to access the IMEI number so after API level 29 if we need some unique id for our application so the android id is the best option other than the IMEI number.

So let's start the coding part.

activity_main.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">

<TextView
android:id="@+id/android_id"
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>

Now we will code for the kotlin file.

MainActivity.kt

package com.example.androidid

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.Settings
import android.view.View
import android.widget.TextView

class MainActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

var tx = findViewById<View>(R.id.android_id) as TextView
var deviceID = Settings.Secure.getString(this.contentResolver, Settings.Secure.ANDROID_ID)

tx.setText("Android id is:: " + deviceID.toString())
}
}

OR

If you want to code for a Java file then here is the code.

MainActivity.java

package com.example.androidid;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.provider.Settings;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
String deviceID;
TextView tx;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tx = (TextView)findViewById(R.id.android_id);
deviceID = Settings.Secure.getString(this.getContentResolver(),Settings.Secure.ANDROID_ID);

tx.setText("Android id is:: "+deviceID.toString());
}
}
Android ID.

That’s it.

Find the source code.

Thank you Happy Coding :)

--

--

Harshita Bambure
Harshita Bambure

Written by Harshita Bambure

Android Developer || WomenTech Global Ambassador at WomenTech Network. || Yoga Teacher || Member @WTM .

No responses yet