Welcome folks today in this blog post we will be seeing how to display system hardware and system cpu info
inside the textview
widget in java. All the full source code of the application is shown below.
Get Started
In order to get started you need to make a new android
project inside the android studio and then you will see the below directory
structure as shown below at the end
of the project.
Editing the Main Layout
Now we need to edit the activity_main.xml
file for the project where we will have the simple TextView
widget in the RelativeLayout as shown below
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?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" tools:context=".MainActivity"> <TextView android:id="@+id/config_details_text_view" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" android:textSize="18sp" /> </RelativeLayout> |
And now we need to edit the MainActivity.java
file and copy paste the following code
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.example.randomimage; import android.os.Bundle; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private TextView configDetailsTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); configDetailsTextView = findViewById(R.id.config_details_text_view); String configDetails = PhoneConfigDetails.getConfigurationDetails(); configDetailsTextView.setText(configDetails); } } |
As you can see in the above java
code we are calling the method
which is defined inside the PhoneConfigDetails
class so now we need to define that as shown below
PhoneConfigDetails.java
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 |
package com.example.randomimage; import android.os.Build; import android.util.Log; public class PhoneConfigDetails { private static final String TAG = "PhoneConfigDetails"; public static String getConfigurationDetails() { StringBuilder builder = new StringBuilder(); builder.append("Build Info:\n\n"); builder.append("Manufacturer: ").append(Build.MANUFACTURER).append("\n"); builder.append("Model: ").append(Build.MODEL).append("\n"); builder.append("Device: ").append(Build.DEVICE).append("\n"); builder.append("Product: ").append(Build.PRODUCT).append("\n"); builder.append("Board: ").append(Build.BOARD).append("\n"); builder.append("Bootloader: ").append(Build.BOOTLOADER).append("\n"); builder.append("CPU ABI: ").append(Build.CPU_ABI).append("\n"); builder.append("CPU ABI2: ").append(Build.CPU_ABI2).append("\n"); builder.append("Display: ").append(Build.DISPLAY).append("\n"); builder.append("Fingerprint: ").append(Build.FINGERPRINT).append("\n"); builder.append("Hardware: ").append(Build.HARDWARE).append("\n"); builder.append("Host: ").append(Build.HOST).append("\n"); builder.append("ID: ").append(Build.ID).append("\n"); builder.append("Product: ").append(Build.PRODUCT).append("\n"); builder.append("Tags: ").append(Build.TAGS).append("\n"); builder.append("Type: ").append(Build.TYPE).append("\n"); builder.append("User: ").append(Build.USER).append("\n"); builder.append("Time: ").append(Build.TIME).append("\n\n"); builder.append("System Properties:\n\n"); builder.append(getSystemProperties()); String configDetails = builder.toString(); Log.d(TAG, configDetails); return configDetails; } private static String getSystemProperties() { StringBuilder builder = new StringBuilder(); String[] properties = { "ro.product.brand", "ro.product.name", "ro.product.device", "ro.product.board", "ro.build.version.release", "ro.build.version.sdk", "ro.build.date.utc", "ro.build.date", "ro.build.type", "ro.build.user", "ro.build.host", "ro.build.tags", "ro.build.fingerprint" }; for (String property : properties) { String value = System.getProperty(property); if (value != null) { builder.append(property).append(": ").append(value).append("\n"); } } return builder.toString(); } } |
As you can see for fetching all this hardware
and cpu
information of the mobile phone we are using the Build
class which is provided by the core android
library and then we are displaying these details as shown below