In this tutorial we will design a very simple application to fetch some basic details of any android phone including Model number and IMEI In Android Studio using Kotlin language. We are going to fetch the following details.
1- Current time Of The Android Device.
2- Current Date Of The Device.
3- If Wifi is connected OR Not.
4- Phone Model.
5- SDK Version.
6- Device Name.
7- Product
8- IMEI Number.
So lets start designing our application Model number and IMEI In Android Studio.
Model Number and IMEI In Android Kotlin:
1- Start a new Android Studio Project and name it as “PhoneDetails” or you can name it whatever you want. Select API level 19 and select the default language as Kotlin.
2- First of all go to your AndroidManifest.xml file and add these two permissions.
1 2 |
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> |
3- Go to activity_main.xml and change the default Constraint Layout to Relative Layout. Set the gravity of your Relative layout as ” center “.
4- In our activity_main.xml file we need a couple of TextViews. We are fetching some 8 different types of details so we need 16 TextViews. 8 TextViews will display the name of the details like ( Date, Time, Wifi Status, Phone Model, SDK, Device, Product and IMEI) and the other 8 TextViews will display the actual values.
5- After designing the TextViews and adding required attributes our final activity_main file will look exactly like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
<?xml version="1.0" encoding="utf-8"?> <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" android:gravity="center" tools:context=".MainActivity"> <TextView android:id="@+id/tv_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Date : " android:textSize="20sp" android:textColor="#000000" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" /> <TextView android:id="@+id/show_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/tv_date" android:textStyle="bold" android:textSize="20sp" android:textColor="#009688" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_date" android:text="Time : " android:textSize="20sp" android:textColor="#000000" android:layout_marginTop="10dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" /> <TextView android:id="@+id/show_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_date" android:layout_toRightOf="@+id/tv_time" android:textStyle="bold" android:textSize="20sp" android:textColor="#009688" android:layout_marginTop="10dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" /> <TextView android:id="@+id/tv_wifi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_time" android:text="WiFi Status : " android:textSize="20sp" android:textColor="#000000" android:layout_marginTop="10dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" /> <TextView android:id="@+id/show_wifi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_time" android:layout_toRightOf="@+id/tv_wifi" android:textStyle="bold" android:textSize="20sp" android:textColor="#009688" android:layout_marginTop="10dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" /> <TextView android:id="@+id/tv_phone_model" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_wifi" android:text="Phone Model : " android:textSize="20sp" android:textColor="#000000" android:layout_marginTop="10dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" /> <TextView android:id="@+id/show_phone_model" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_wifi" android:layout_toRightOf="@+id/tv_phone_model" android:textStyle="bold" android:textSize="20sp" android:textColor="#009688" android:layout_marginTop="10dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" /> <TextView android:id="@+id/tv_SDK" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_phone_model" android:text="SDK : " android:textSize="20sp" android:textColor="#000000" android:layout_marginTop="10dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" /> <TextView android:id="@+id/show_SDK" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_phone_model" android:layout_toRightOf="@+id/tv_SDK" android:textStyle="bold" android:textSize="20sp" android:textColor="#009688" android:layout_marginTop="10dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" /> <TextView android:id="@+id/tv_device" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_SDK" android:text="Device : " android:textSize="20sp" android:textColor="#000000" android:layout_marginTop="10dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" /> <TextView android:id="@+id/show_device" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_SDK" android:layout_toRightOf="@+id/tv_device" android:textStyle="bold" android:textSize="20sp" android:textColor="#009688" android:layout_marginTop="10dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" /> <TextView android:id="@+id/tv_product" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_device" android:text="Product : " android:textSize="20sp" android:textColor="#000000" android:layout_marginTop="10dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" /> <TextView android:id="@+id/show_product" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_device" android:layout_toRightOf="@+id/tv_product" android:textStyle="bold" android:textSize="20sp" android:textColor="#009688" android:layout_marginTop="10dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" /> <TextView android:id="@+id/tv_IMEI" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_product" android:text="IMEI : " android:textSize="20sp" android:textColor="#000000" android:layout_marginTop="10dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" /> <TextView android:id="@+id/show_IMEI" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_product" android:layout_toRightOf="@+id/tv_IMEI" android:textStyle="bold" android:textSize="20sp" android:textColor="#009688" android:layout_marginTop="10dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" /> </RelativeLayout> |
MainActivity.kt Code
6- Now we will start our Kotlin code. Come to MainActivity.kt file and here for each detail we will create a separate function.
7- Below our onCreate Method we will define our first method to fetch the current Date of the Android Device. After fetching it we will display it in our TextView. Our displayDate() function will be like this. On clicking the date it will show a Toast message too.
1 2 3 4 5 6 7 8 9 10 11 |
private fun displayDate() { val date = SimpleDateFormat("dd/M/yyyy") val currentDate = date.format(Date()) show_date.setText(currentDate) show_date.setOnClickListener { Toast.makeText(this, currentDate.toString(), Toast.LENGTH_SHORT).show() } } |
8- We will add a separate function to display the current time of the Device in the TextView. Our displayTime() function will look like this.
1 2 3 4 5 6 7 8 9 10 |
private fun displayTime() { val time = SimpleDateFormat("hh:mm:ss") val currentTime = time.format(Date()) show_time.setText(currentTime) show_time.setOnClickListener{ Toast.makeText(this, currentTime.toString(),Toast.LENGTH_SHORT ).show() } } |
9- To check if the wifi is connected or not we will use this method. If the Wifi is connected it will show “Connected” and if it’s not connected it will show “Disconnected”. Our function will look like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
private fun wifiStatus() { val cm = applicationContext. getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager val wifi = cm?. getNetworkInfo(ConnectivityManager.TYPE_WIFI) var status: String if ( wifi.isConnected) { status = "Connected" show_wifi.setText(status) show_wifi.setOnClickListener{ Toast.makeText(this, "Connected",Toast.LENGTH_SHORT ).show() } } else { status = "Disconnected" show_wifi.setText(status) show_wifi.setOnClickListener{ Toast.makeText(this, "Disconnected",Toast.LENGTH_SHORT ).show() } } } |
10- Function to fetch the Model of the Device.
1 2 3 4 5 6 7 8 9 |
private fun phoneModel() { val myDeviceModel: String = android.os.Build.MODEL show_phone_model.setText(myDeviceModel) show_phone_model.setOnClickListener{ Toast.makeText(this, myDeviceModel,Toast.LENGTH_SHORT ).show() } } |
11- This function will fetch the current SDK version.
1 2 3 4 5 6 7 8 9 10 |
private fun SDK() { val mySDK:String = android.os.Build.VERSION.SDK show_SDK.setText(mySDK) show_SDK.setOnClickListener { Toast.makeText(this, mySDK, Toast.LENGTH_SHORT).show() } } |
12- To fetch the Device name.
1 2 3 4 5 6 7 8 9 10 |
private fun device() { val myDevice: String = android.os.Build.DEVICE show_device.setText(myDevice) show_device.setOnClickListener{ Toast.makeText(this, myDevice,Toast.LENGTH_SHORT ).show() } } |
13- To find the Build Product of the Android Device we will use this function.
1 2 3 4 5 6 7 8 9 10 |
private fun product() { val myBuildProduct: String = android.os.Build.PRODUCT show_product.setText(myBuildProduct) show_product.setOnClickListener{ Toast.makeText(this, myBuildProduct,Toast.LENGTH_SHORT ).show() } } |
14- For finding the IMEI number of the device we use a try and catch block. If successful the try block will fetch the IMEI number of the device if it didn’t it would return an exception. It asks for the user permission first. This is how our function will look like.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
@RequiresApi(Build.VERSION_CODES.O) private fun IMEI() { var myIMEI: String? = null try { val tm = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager val IMEI = tm.getImei() if (IMEI != null) { myIMEI = IMEI show_IMEI.setText(myIMEI) show_IMEI.setOnClickListener{ Toast.makeText(this, myIMEI,Toast.LENGTH_SHORT ).show() } } } catch (ex:Exception) { Toast.makeText(this, ex.toString(),Toast.LENGTH_SHORT ).show() } if (ContextCompat.checkSelfPermission(this, android.Manifest.permission. READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat. shouldShowRequestPermissionRationale(this, android.Manifest.permission. READ_PHONE_STATE)) { } else { ActivityCompat. requestPermissions(this, arrayOf(android.Manifest. permission.READ_PHONE_STATE), 2) } } } |
15- Now, we have 8 different functions for 8 different details. We make another function with name allFunctions() and call all our 8 functions in this function, just like this.
1 2 3 4 5 6 7 8 9 10 11 12 |
@RequiresApi(Build.VERSION_CODES.O) private fun allFunctions() { displayDate() displayTime() wifiStatus() phoneModel() SDK() device() product() IMEI() } |
onCreate() Function
16- Now finally, we just have to call one function in our onCreate function. Like this.
1 2 3 4 5 6 7 8 9 |
@RequiresApi(Build.VERSION_CODES.O) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) allFunctions() } |
17- Here we are done, our complete final MainActivity.kt file is this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
package com.example.kotlinpractice import android.content.Context import android.content.pm.PackageManager import android.net.ConnectivityManager import android.os.Build import android.os.Bundle import android.telephony.TelephonyManager import android.widget.Toast import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import kotlinx.android.synthetic.main.activity_main.* import java.text.SimpleDateFormat import java.util.* class MainActivity : AppCompatActivity() { @RequiresApi(Build.VERSION_CODES.O) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) allFunctions() } private fun displayDate() { val date = SimpleDateFormat("dd/M/yyyy") val currentDate = date.format(Date()) show_date.setText(currentDate) show_date.setOnClickListener { Toast.makeText(this, currentDate.toString(), Toast.LENGTH_SHORT).show() } } private fun displayTime() { val time = SimpleDateFormat("hh:mm:ss") val currentTime = time.format(Date()) show_time.setText(currentTime) show_time.setOnClickListener{ Toast.makeText(this, currentTime.toString(),Toast.LENGTH_SHORT ).show() } } private fun wifiStatus() { val cm = applicationContext. getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager val wifi = cm?. getNetworkInfo(ConnectivityManager.TYPE_WIFI) var status: String if ( wifi.isConnected) { status = "Connected" show_wifi.setText(status) show_wifi.setOnClickListener{ Toast.makeText(this, "Connected",Toast.LENGTH_SHORT ).show() } } else { status = "Disconnected" show_wifi.setText(status) show_wifi.setOnClickListener{ Toast.makeText(this, "Disconnected",Toast.LENGTH_SHORT ).show() } } } private fun phoneModel() { val myDeviceModel: String = android.os.Build.MODEL show_phone_model.setText(myDeviceModel) show_phone_model.setOnClickListener{ Toast.makeText(this, myDeviceModel,Toast.LENGTH_SHORT ).show() } } private fun SDK() { val mySDK:String = android.os.Build.VERSION.SDK show_SDK.setText(mySDK) show_SDK.setOnClickListener { Toast.makeText(this, mySDK, Toast.LENGTH_SHORT).show() } } private fun device() { val myDevice: String = android.os.Build.DEVICE show_device.setText(myDevice) show_device.setOnClickListener{ Toast.makeText(this, myDevice,Toast.LENGTH_SHORT ).show() } } private fun product() { val myBuildProduct: String = android.os.Build.PRODUCT show_product.setText(myBuildProduct) show_product.setOnClickListener{ Toast.makeText(this, myBuildProduct,Toast.LENGTH_SHORT ).show() } } @RequiresApi(Build.VERSION_CODES.O) private fun IMEI() { var myIMEI: String? = null try { val tm = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager val IMEI = tm.getImei() if (IMEI != null) { myIMEI = IMEI show_IMEI.setText(myIMEI) show_IMEI.setOnClickListener{ Toast.makeText(this, myIMEI,Toast.LENGTH_SHORT ).show() } } } catch (ex:Exception) { Toast.makeText(this, ex.toString(),Toast.LENGTH_SHORT ).show() } if (ContextCompat.checkSelfPermission(this, android.Manifest.permission. READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat. shouldShowRequestPermissionRationale(this, android.Manifest.permission. READ_PHONE_STATE)) { } else { ActivityCompat. requestPermissions(this, arrayOf(android.Manifest. permission.READ_PHONE_STATE), 2) } } } @RequiresApi(Build.VERSION_CODES.O) private fun allFunctions() { displayDate() displayTime() wifiStatus() phoneModel() SDK() device() product() IMEI() } } |
OUTPUT


People Also Read:
Enable or Disable Wifi In Android Programmatically In Kotlin
Kotlin Text To Speech Working Example
How To Check Internet Connection in Android Programmatically