56

How can I get the device name programmatically in Android?

Caner
  • 57,267
  • 35
  • 174
  • 180
  • 2
    check out this link http://developer.android.com/reference/android/os/Build.html – Aravindhan Jul 05 '11 at 08:46
  • 4
    possible duplicate of [Get Android Phone Model Programmatically](http://stackoverflow.com/questions/1995439/get-android-phone-model-programmatically) – kabuko Jun 13 '12 at 17:21

9 Answers9

76

To display the device name/model in android use:

TextView tv1 = (TextView) findViewById(R.id.tv1);
String str = android.os.Build.MODEL;
tv1.setText(str);

Link to Android Build Class

cjds
  • 8,268
  • 10
  • 49
  • 84
Rahul Sharma
  • 3,637
  • 2
  • 20
  • 18
31
android.os.Build.MANUFACTURER + android.os.Build.MODEL
Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85
diptia
  • 2,135
  • 21
  • 21
  • 3
    In Google Nexus devices that were made by LG - this will give you the wrong string - "LGE Nexus 5". You should use android.os.Build.BRAND + android.os.Build.MODEL. – FunkSoulBrother Nov 11 '15 at 09:02
30

You can get the device name from android.provider.Settings using :

Settings.Global.getString(context.getContentResolver(), "device_name")
Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
  • 1
    This should be the accepted answer as it provides the actual device name that the user can modify. The other answers give the device manufacturer name and device model. – ljtomev Apr 13 '21 at 07:56
  • what is the min Android version support this? – Amos Aug 22 '23 at 04:39
  • Settings.Global.getString(requireContext().contentResolver, Settings.Global.DEVICE_NAME), Android Studio tells min API level is 25 – Amos Aug 22 '23 at 04:53
12

As others mentioned, you can use Build.MODEL, Build.DEVICE and you can get lot more info about device. Go to developer.android.com to read more about Build class. BTW remember to import/add import android.os.Build;.

A simple example:

StringBuffer infoBuffer = new StringBuffer();

infoBuffer.append("-------------------------------------\n");
infoBuffer.append("Model :" + Build.MODEL + "\n");//The end-user-visible name for the end product.
infoBuffer.append("Device: " + Build.DEVICE + "\n");//The name of the industrial design.
infoBuffer.append("Manufacturer: " + Build.MANUFACTURER + "\n");//The manufacturer of the product/hardware.
infoBuffer.append("Board: " + Build.BOARD + "\n");//The name of the underlying board, like "goldfish".
infoBuffer.append("Brand: " + Build.BRAND + "\n");//The consumer-visible brand with which the product/hardware will be associated, if any.
infoBuffer.append("Serial: " + Build.SERIAL + "\n");
infoBuffer.append("-------------------------------------\n");
    /* Android doc:
        This 'Serial' field was deprecated in API level O.
        Use getSerial() instead.
        A hardware serial number, if available.
        Alphanumeric only, case-insensitive. For apps targeting SDK higher than N_MR1 this field is set to UNKNOWN.
    */

//I just used AlertDialog to show device information
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setCancelable(true);
dialog.setTitle("Device information:");
dialog.setMessage(infoBuffer);//StringBuffer which we appended the device informations.
dialog.show();
Blasanka
  • 21,001
  • 12
  • 102
  • 104
6

To get the phone name, for instance: Galaxy S5 or the like, just add this to your dependencies:

compile 'com.jaredrummler:android-device-names:1.0.9'

Then sync your project, when it finishes, go to your Java class and use this code:

String mDeviceName = DeviceName.getDeviceName();

And that's it.

Community
  • 1
  • 1
6

In order to get android device name you have to add only a single line of code:

android.os.Build.MODEL;

Copy and paste the following code in your projects onCreate():

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv1 = (TextView) findViewById(R.id.tv1);
String str = android.os.Build.MODEL;
tv1.setText(str);
cjds
  • 8,268
  • 10
  • 49
  • 84
Rupok
  • 2,062
  • 16
  • 16
4

The name and model of your Android Device can be found like this:

String manufacturerModel = android.os.Build.MANUFACTURER + " " + android.os.Build.MODEL;
  • Build.Model often is a kind of cryptic Model name; eg Samsung SM-T395 is a TabActive 2. So the answer ist precise but not very user friendly if this is used to display to user. – FrankKrumnow Feb 23 '23 at 14:44
2

Using following way can get the device name and also in case user modify the device name, it can also get the updated name:

Settings.Secure.getString(getContentResolver(), “bluetooth_name”); 

Because bluetooth_name is not a documented API, in case it returns null, you can use following code instead: (note that this API is only available since Android 7.1 and per my testing, it CANNOT get the device name immediately after user modified)

Settings.System.getString(getContentResolver(), “device_name”);
shizhen
  • 12,251
  • 9
  • 52
  • 88
0

Here is how to get the name that the user set in the "About Phone" "Device name" screen using adb:

$ adb shell 'settings get global device_name'