How to get Android Device Hardware Information

How to get Android Device Hardware Information

In this tutorial we are going to learn how to get Android device hardware information. We will focus on the following hardware properties like Android SDK, Android Version, Android Brand, Manufacturer’s name and Android model.

This information is different from device to device. There are many other information you can get from a device but we will focus on the these five properties.

To create this application, we are going to use a ListView control with a custom adapter that will hold each item of information.

In order to understand what we are out to achieve, we are going to have a look at the screen shot of the application we will create below.

mhardware

Before we start, it is important for you to understand the tools and environment I used in this application development tutorial. Feel free to use any tools you are familiar with.

Windows 7

Android Studio

Sony Xperia ZL

Min SDK 14

Target SDK 19

To create a new android application project, following the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: AndroidDeviceHardware

Package: com.inducesmile.androiddevicehardware

Keep other default selections.

Continue to click on next button until Finish button is active, then click on Finish Button.

If you are using Android Studio as your choice IDE, the new project will create a default

MainActivity.java file and its corresponding layout file. Open the two files because we are going to

make use of them.

Now, open the activity_main.xml layout, we are going to add a ListView control. The code snippet layout code is shown below.

<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">

    <ListView
        android:id="@+id/android_hardware"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="8dp"
        android:scrollbars="none" />

</RelativeLayout>

As you can see, the layout code is very simple and does not even need explanation. Now that will have done with the layout file, We will head over on the MainActivty.java file to create the corresponding code that will retrieve the device information.

In the MainActivity.java file, we will get the handle for the ListView component. We will call the setAdapter() method of the ListView instance and the Adapter instance will be passed as parameter.

Since we haven’t created the Adapter class, I think this is the best time to create the class. The Adapter class will inflate a layout file that will house two TextView controls. The code snippet is as shown.

package inducesmile.com.completeandroid.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import inducesmile.com.completeandroid.R;

public class HardwareAdapter extends BaseAdapter {

    private LayoutInflater layoutInflater;
    private List<HardwareObject> listStorage;

    public HardwareAdapter(Context context, List<HardwareObject> customizedListView) {
        layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        listStorage = customizedListView;
    }

    @Override
    public int getCount() {
        return listStorage.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder listViewHolder;
        if(convertView == null){
            listViewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.hardware_cardview, parent, false);

            listViewHolder.hardware = (TextView)convertView.findViewById(R.id.header);
            listViewHolder.hardwareMeaning = (TextView)convertView.findViewById(R.id.header_value);
            convertView.setTag(listViewHolder);
        }else{
            listViewHolder = (ViewHolder)convertView.getTag();
        }
        listViewHolder.hardware.setText(listStorage.get(position).getHardware());
        listViewHolder.hardwareMeaning.setText(listStorage.get(position).getHardwareMeaning());

        return convertView;
    }

    static class ViewHolder{

        TextView hardware;
        TextView hardwareMeaning;
    }
}

You can see that in the getView(int position, View convertView, ViewGroup parent) method of the Adapter class, a layout file was inflate. The Adapter class also contains an inner class called ViewHolder which holds the instances of the TextView components.

The code for the inflated layout file – hardware_cardview.xml is shown below.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="8dp"
    android:layout_marginBottom="8dp"
    card_view:cardUseCompatPadding="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp">

        <TextView
            android:id="@+id/header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world"
            android:textColor="@color/primary_text"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"/>

        <TextView
            android:id="@+id/header_value"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world"
            android:layout_alignTop="@+id/header"
            android:textColor="@color/primary_text"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

    </RelativeLayout>

</android.support.v7.widget.CardView>

Bear in mind that this layout file uses a CardView as the GroupView in the layout. In other to use a CardView component in your application, we are going to add the following line of code in our project build.gradle file.

compile 'com.android.support:cardview-v7:21.0.+'

What it does is it fetch the library and add it in our project so that we can have access to it and its numerous properties. As you can see, it is two TextViews arrange side by side to each other.

Finally, we will look at our MainActivity.java file once again since we have explain and code all the different parts we need for the application.

The code snippet for our MainActivity.java file as shown below.

package inducesmile.com.completeandroid;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import inducesmile.com.completeandroid.adapters.HardwareAdapter;
import inducesmile.com.completeandroid.adapters.HardwareObject;

public class MainActivity extends Activity {
   
    @Override
    public View onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_article);       
        List<HardwareObject> mObject = getHardwareInformation();
        ListView androidList = (ListView)view.findViewById(R.id.android_hardware);
        HardwareAdapter hAdapter = new HardwareAdapter(getActivity(), mObject);
        androidList.setAdapter(hAdapter);
        return view;
    }
    private List<HardwareObject> getHardwareInformation(){
        List<HardwareObject> hardware = new ArrayList<HardwareObject>();
        String[] mAndroid = getAndroidHardWare();
        hardware.add(new HardwareObject("Android SDK", mAndroid[0]));
        hardware.add(new HardwareObject("Android Version", mAndroid[1]));
        hardware.add(new HardwareObject("Android Brand", mAndroid[2]));
        hardware.add(new HardwareObject("Manufacturer", mAndroid[3]));
        hardware.add(new HardwareObject("Android Model", mAndroid[4]));
        return hardware;
    }
    private String[] getAndroidHardWare(){
        String androidSDK = String.valueOf(android.os.Build.VERSION.SDK_INT);
        String androidVersion = android.os.Build.VERSION.RELEASE;
        String androidBrand = android.os.Build.BRAND;
        String androidManufacturer = android.os.Build.MANUFACTURER;
        String androidModel = android.os.Build.MODEL;
        return new String[]{androidSDK, androidVersion, androidBrand, androidManufacturer, androidModel};
    }
}

Apart from the Adapter class that we discuss previously, there are two other method we created in the MainActivity.java class – getHardwareInformation() and getAndroidHardWare()

When you look at the getHardwareIndformation() method, you will see that we made use of an entity object class called HardwareObject. This instance of the class wraps two corresponding TextView and it is added in a List object.

In the onCreate(Bundle savedInstanceState) method of the class, the getHardwareInformation() method returns the values that is passed as a parameter in the Adapter class.

Now, when you run your application you will see the interface that looks similar to the sample that was shown earlier on.

You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact me.

Remember to subscribe with your email so that you will be among the first to receive our new post once it is published.

If you like this tutorial, please kindly download my new android app in Google play store by clicking the image below

One Response

Add a Comment