Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Android Mobile or Tablet user_agents #1236

hafizbistar opened this Issue Apr 2, 2012 · 11 comments


None yet
5 participants

not sure how to implement it, but it would be better if the user_agents could differentiates between android mobile and tablet.
note:as far as i know (with my very limited knowledge), tablet devices still uses media="screen" instead of media="handheld"


At the moment, i extend the User_agent.php library as MY_User_agent.php

added all the private functions, and slightly edit the _set_mobile()

if (FALSE !== (strpos(strtolower($this->agent), $key)))
    $this->mobile = $val;
    if($val == 'Android' && ! (strpos(strtolower($this->agent), 'Mobile')))

    $this->is_mobile = TRUE;
    return TRUE;

narfbg commented Oct 5, 2012

Sorry, but a tablet IS a mobile device.

@narfbg narfbg closed this Oct 5, 2012

well.. from my point of view, we want to know the user agent mainly because we want to customize the CSS of certain devices.
Tablet devices does not read from "handheld" media type instead it reads from "screen". Some smart phones doesn't either (possibly due to the huge resolution it supports).

But regardless of the changes of this thread status, i don't think its no longer necessary.
Now the hot keyword is responsive design. ;)


narfbg commented Oct 17, 2012

If you can come up with a new (and reasonable) feature for that - that would be great, but is_mobile has other purposes.

Actually, I have no clue of what other purpose to detect the user_agent.
sorry, can't be much help.


dchill42 commented Oct 17, 2012

This sounds like it should be a media_type() call. Can that be reliably determined from the user agent string?


narfbg commented Mar 14, 2013

Has been done already.

could you please provide me some info on how to differentiate between android tablet and android phone, do we have another function in user_agent library?

maybe useful to you:

take note that it is not a complete solution.

gablem commented Apr 28, 2015

This discussion inspired me to create my own MY_User_agent class. In order to use halfizbistar's code considering narfbg's argument, I decided to add new methods tablet() and is_tablet(). Later, I noticed that what I trully wanted to know about the agent was whether it had a small enough screen to justify a redirection to the mobile website. So I also added methods has_large_screen() and has_small_screen().

Here's the code:
// Add this to the user_agents.php config file
$tablets = array(
'ipad' => 'iPad',
'android' => 'Android', // further check in the MY_User_agent library to make sure it's not a phone

// Create the MY_User_agent.php file in the app/libraries directory

tablets = $tablets; unset($tablets); $return = TRUE; } return $return; } // -------------------------------------------------------------------- /** * Is Tablet * * @Param string $key * @return bool */ public function is_tablet($key = NULL) { if ( ! $this->is_tablet) { return FALSE; } // No need to be specific, it's a tablet if ($key === NULL) { return TRUE; } // Check for a specific tablet return (isset($this->tablets[$key]) && $this->tablet === $this->tablets[$key]); } // Returns whether the agent is known to have a large screen (greather than or equal to 640 (virtual) pixels wide) public function has_large_screen() { return ! $this->is_mobile(); // mobile includes tablets } // Returns whether the agent is known to have a small screen (less than 640 (virtual) pixels wide) public function has_small_screen() { return $this->is_mobile && ! $this->is_tablet; } // -------------------------------------------------------------------- /** * Parse a custom user-agent string * * @Param string $string * @return void */ public function parse($string) { $this->is_tablet = FALSE; $this->tablet = ''; parent::parse($string); } // -------------------------------------------------------------------- /** * Compile the User Agent Data * * @return bool */ protected function _compile_data() { $this->_set_platform(); foreach (array('_set_robot', '_set_browser', '_set_mobile', '_set_tablet') as $function) { if ($this->$function() === TRUE) break; } } // -------------------------------------------------------------------- /** * Set the Mobile Device * * @return bool */ protected function _set_mobile() { if ($retval = parent::_set_mobile()) $this->_set_tablet(); return $retval; } // -------------------------------------------------------------------- /** * Set the Tablet Device * * @return bool */ protected function _set_tablet() { if (is_array($this->tablets) && count($this->tablets) > 0) { foreach ($this->tablets as $key => $val) { if (FALSE !== (stripos($this->agent, $key))) { if ($val == 'Android' && FALSE !== strpos($this->agent, 'Mobile')) continue; $this->is_tablet = TRUE; $this->tablet = $val; return TRUE; } } } return FALSE; } ``` } [/code]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment