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.

Example:

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

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"?>
<resources>
  <string name="screen_type">7-inch-tablet</string>
</resources>

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"?>
<resources>
  <string name="screen_type">10-inch-tablet</string>
</resources>
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:
http://developer.android.com/guide/practices/screens_support.html 
http://developer.android.com/training/multiscreen/screensizes.html#TaskUseSWQuali 

Comments

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