how to get device model name programmatically in Android Kotlin


Contents : Detect a android device manufacturer name and phone model programmatically
1. How to get device name programmatically in Kotlin (using Kotlin function)
2. Android get device model name  in Android (using Android/Java function)
3. Android get device manufacturer information programmatically

Android.os.Build.MODEL

public static final String MODEL,  Added in API level 1
The end-user-visible name for the end product.

Android java function getDeviceName() code:
Utils.java :
public class Utils {
    public static String getDeviceName(){
        String deviceName = android.os.Build.MODEL; // returns model name
        return deviceName;
    }
}

Using kotlin version function getPhoneDeviceName() code:
K_utils.kt :
import android.os.Build
class K_utils {
    companion object {
        @JvmStatic
        fun getPhoneDeviceName():String {
            val model = Build.MODEL // returns model name
            return model;
        }

    }
}

MainActivity.kt:
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import com.example.espl.myapplication.K_utils.Companion.getPhoneDeviceName
import com.example.espl.myapplication.Utils.getDeviceName

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

        val tv_java_utils_output: TextView = findViewById(R.id.tv_java_utils_output) as TextView
        //using java function getDeviceName()
            tv_java_utils_output.text = "Using java Function::"+getDeviceName();

        //using kotlin function getPhoneDeviceName()
        val tv_kotlin_utils_output: TextView = findViewById(R.id.tv_kotlin_utils_output) as TextView
            tv_kotlin_utils_output.text ="Using Kotlin version of Function::"+getPhoneDeviceName();
    }
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        tools:context=".MainActivity">

    <TextView
            android:gravity="center_vertical|center_horizontal"
            android:layout_width="wrap_content"
            android:id="@+id/tv_java_utils_output"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:background="@color/colorGray"
            android:layout_margin="10dp"
            android:textColor="@color/colorPrimary"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

    <TextView
            android:padding="10dp"
            android:layout_margin="10dp"
            android:textColor="@color/colorPrimary"
            android:id="@+id/tv_kotlin_utils_output"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorGray"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

</LinearLayout>

Output on HTC Desire 620G dual sim phone device model:
android.os.Build.MODEL- get device model name programmatically in Android Kotlin
get android mobile phone model name programmatically - Kotlin
android.os.Build.MANUFACTURER returns public static final String MANUFACTURER - Added in API level 4
The manufacturer of the product/hardware.

Similarly to get the device manufacturer name in android programmatically use below code that returns a manufacturer name as a string.

What is my device manufacturer - detect a android device manufacturer name

String manufacturer = Build.MANUFACTURER;
//For HTC Desire 620G dual sim phone device it returns HTC

To get device information programmatically such as manufacturer details refer: https://developer.android.com/reference/android/os/Build#MANUFACTURER


No comments:

Post a Comment

Popular Posts