Code to identify device is small(phone/handset) or large(Tablet) in Android

Here the difference between a tablet and a phone is the screen size which is why you want to use different layout files. These files are stored in the res/layout-<qualifiers> directories. You can create an XML file in the directoy res/values-<same qualifiers> for each of your layouts and put an int/bool/string resource into it to distinguish between the layouts you use.


File res/values/screen.xml (assuming res/layout/ contains your layout files for handsets)
<?xml version="1.0" encoding="utf-8"?>
  <string name="screen_type">phone</string>

File res/values-sw600dp/screen.xml (assuming res/layout-sw600dp/ contains your layout files for small tablets like the Nexus 7)
<?xml version="1.0" encoding="utf-8"?>
  <string name="screen_type">7-inch-tablet</string>

File res/values-sw720dp/screen.xml (assuming res/layout-sw720dp/ contains your layout files for large tablets like the Nexus 10):
<?xml version="1.0" encoding="utf-8"?>
  <string name="screen_type">10-inch-tablet</string>
Now the screen type is accessible using the R.string.screen_type constant.

Another easiest way of detecting device type is to put different values in strings.xml for a single key, let's call it "device_type". So in:
  1. values -> strings.xml I will have <string name="device_type">smartphone</string>
  2. values-large -> strings.xml I will have <string name="device_type">tablet</string>
  3. values-small -> strings.xml I will have <string name="device_type">small</string>
  4. values-xlarge -> strings.xml I will have <string name="device_type">xlarge</string>
Having a context, I will read this value and determine what type of device I have.

Please refer the folllowing URl's for more information: 


Popular posts from this blog

Custom Dialog with no title and full screen in Android

code to display .pdf,.text,.DOC,.DOCX,.doc extension files exists in SDCard

Best working code to set Date limits on Date Picker Dialog,in Android