Monday, 30 July 2012

Programatically to find whether a device is mobile or tablet in Android

we can use the OS version to get that but there is a problem when the android 4.0 came . Because there are both tablet and mobile in OS 4.0 . So it is hard to find the device type using OS version

public class MobileorTabletActivity extends Activity {
 WebView web1;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        web1 = new WebView(this);
        String useragent = web1.getSettings().getUserAgentString();
        System.out.println(useragent);
        if(useragent.toLowerCase().contains("mobile"))
        {
         Toast.makeText(this, "Mobile", Toast.LENGTH_LONG).show();
        }
        else
        {
         Toast.makeText(this, "Tablet", Toast.LENGTH_LONG).show();
        }
    }
 }

                If the device is mobile then the Webview default userAgentString  contains "mobile". but if it is tablet there is no "mobile" string.

No comments:

Post a Comment