StackTips

Get Device ID Example in Android

nilan avtar

Written by

Nilanchala,  2 min read,  3K views, updated on Sept. 17, 2023

This example explains how to get device unique id in android. The device unique id is needed when we want user registration for a specific device. This way we can achieve security. Most of the banking applications today are using such approach.

Android Activity Layout (activity_main.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="#cc0000"
        android:textSize="32sp" />

</RelativeLayout>

Android Activity (MainActivity.java)

package com.javatechig.getdeviceid;

import android.app.Activity;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.widget.TextView;

public class MainActivity extends Activity {

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

		//getting unique id for device
		String id = Secure.getString(getContentResolver(), Secure.ANDROID_ID);

		//displaying id in textview
		TextView tv = (TextView) findViewById(R.id.textView1);
		tv.setText(id);

	}

}

Output

Get Device ID Example in Android