A Mobile is a portable electronic device that allows you to make calls, send messages, and access the internet, among other functions. A tablet is a mobile computing device with a touchscreen display and typically a larger screen size than a smartphone. Both devices are designed to be portable and allow you to stay connected while on the go. Here, we will discuss how to detect the user using a mobile or tablet device for the purpose. This is possible using programmatically in android studio.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2:
Here is the AndroidManifest.xml file
XML
<? xml version = "1.0" encoding = "utf-8" ?> < application android:allowBackup = "true" android:dataExtractionRules = "@xml/data_extraction_rules" android:fullBackupContent = "@xml/backup_rules" android:icon = "@mipmap/ic_launcher" android:label = "@string/app_name" android:roundIcon = "@mipmap/ic_launcher_round" android:supportsRtl = "true" android:theme = "@style/Theme.PhoneOrTabletGFG" tools:targetApi = "31" > < activity android:name = ".MainActivity" android:exported = "true" > < intent-filter > < action android:name = "android.intent.action.MAIN" /> < category android:name = "android.intent.category.LAUNCHER" /> </ intent-filter > < meta-data android:name = "android.app.lib_name" android:value = "" /> </ activity > </ application > </ manifest > |
Step 3: Make a design to show the device type in the resource layout file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" > < androidx.appcompat.widget.AppCompatButton android:id = "@+id/button" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Check Device Type" android:backgroundTint = "#43DC07" android:textSize = "25dp" android:textColor = "@color/black" android:layout_centerInParent = "true" /> < TextView android:id = "@+id/checkphonetablet" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Device Type" android:textSize = "25sp" android:textColor = "@color/black" app:layout_constraintBottom_toBottomOf = "parent" app:layout_constraintLeft_toLeftOf = "parent" app:layout_constraintRight_toRightOf = "parent" app:layout_constraintTop_toTopOf = "parent" android:layout_below = "@+id/button" android:layout_centerHorizontal = "true" android:layout_marginTop = "20dp" /> </ RelativeLayout > |
UI:
Step 4: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
package com.example.phoneortabletgfg; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.content.res.Configuration; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = findViewById(R.id.checkphonetablet); Button buttonX = (Button)findViewById(R.id.button); buttonX.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { textView.setText(getDeviceInfo(getApplicationContext(), Device.DEVICE_TYPE)); textView.setVisibility(View.VISIBLE); } }); } public enum Device { DEVICE_TYPE, } public static String getDeviceInfo (Context context, Device device) { try { if (device == Device.DEVICE_TYPE) { if (isTablet(context)) { if (getDevice5Inch(context)) { return "This is Tablet" ; } else { return "This is Mobile" ; } } else { return "This is Mobile" ; } } } catch (Exception e) { e.printStackTrace(); } return "" ; } private static boolean getDevice5Inch(Context context) { try { DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); float yinch = displayMetrics.heightPixels / displayMetrics.ydpi; float xinch = displayMetrics.widthPixels / displayMetrics.xdpi; double diagonalinch = Math.sqrt(xinch * xinch + yinch * yinch); if (diagonalinch >= 7 ) { return true ; } else { return false ; } } catch (Exception e) { return false ; } } private static boolean isTablet(Context context) { return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE; } } |
Now all are set, just run your app and you will be able to see the device type of your device.