Detecting SmartPhones Not Tablets - mobile

I'm creating a mobile version of a website, but only for smartphones. If the device is a tablet, notebook, netbook, desktop, or whatever similar device, I want the user to automatically go to the full site. If it was simple CSS, I would know how to do it. However, the mobile version uses different templates and scripts than the full-site version, and needs to be redirected accordingly rather than simply use another stylesheet.
Anybody have a suggestion or article that can show how to detect said devices and redirect accordingly?

A working solution to your problem can be found here: https://github.com/sebarmeli/JS-Redirection-Mobile-Site

The only way I could find to do this was a hack in which I:
Used css media queries to detect phones using max-device-width
Set a change in style on this basis - I changed body color from #000000 to #000001
Use an OnLoad to call a Javascript to check the body color.
Redirect on this basis. (In my own case I actually used a window.confirm call to offer the user a choice, rather than forcing redirection).
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Phone test</title>
<meta name="viewport" content="width=device-width">
<style type="text/css">
body
{ color: rgb(0, 0, 0);}
#media screen and (max-device-width:600px), screen and (max-width:600px)
{
body{ color: rgb(0, 0, 1);}
}
</style>
<script type="text/javascript">
<!--
function doPhoneCheck()
{
var col;
if(document.body.currentStyle)
{
col = document.body.currentStyle.color; // IE
}
else
{
col = getComputedStyle(document.body).getPropertyValue("color");
}
if(col =="rgb(0, 0, 1)")
{
window.location.href="http://www.apple.com/";
}
}
//-->
</script>
It detects all iPhones to 6+, and the Android and Windows phones I've tested. It discriminates against all current iPads and against the laptops and desktops I've tried.
NB
(1) For color you must use rgb and leave spaces exactly as written.
(2) I found similar code would not detect body background-color for some unknown reason.
(3) The key to detecting the css style is to use getComputedStyle() (or currentStyle for IE) rather than getStyle().
(4) Even though the change in body color is imperceptible, it can be over-ridden by specifying colors for all your css classes etc.

Related

Displaying of html text in codenameone is very small

I am using browser component to display HTML text in my app. Everything was working fine before, but in recent build the font size displayed in browser component is very small.It works fine on simulator but on device it looks very small. Here is my test case and is tested on iPhone XS , iPhone 7 and iPad.
Form f= new Form();
f.setLayout(new BorderLayout());
BrowserComponent browser = new BrowserComponent();
Container cont = new Container();
cont.setLayout(new BorderLayout());
cont.addComponent(BorderLayout.CENTER, browser);
String data = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>Test</title><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" /></head><body><p>Hello World</p></body></html>";
browser.setPage(data, "");
cont.setPreferredH(com.codename1.ui.Display.getInstance().getDisplayHeight()/2);
browser.setPreferredH(com.codename1.ui.Display.getInstance().getDisplayHeight()/2);
f.addComponent(BorderLayout.CENTER,browser);
f.show();
Let me know what is been changed and how I can increase the size of font .
Thanks
I add an info to the Shai's answer about the font size.
Try to add the following meta tags:
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="MobileOptimized" content="width" />
<meta name="HandheldFriendly" content="true" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="cleartype" content="on" />
Hopefully these meta tags fixes the font sizes on real devices. In the CSSes used in web pages, I suggest to use the em as unit to specify the font sizes, because its mobile-device-friendly nature (independent from the dpi of the screen device).
Addendum: to don't confuse, while em is fine for CSSes used in web pages (inside a BrowserComponent), in the Codename One CSSes used to style Components it's better to use mm or pt as unit independent from dpi.
Apple started sending out warning emails to everyone who uses UIWebView so we toggled the switch to use WKWebView. This is something we wrote about a while back here: https://www.codenameone.com/blog/wkwebview.html
You generally shouldn't switch back but you should add a stylesheet to your HTML to explicitly determine the font size. That's the right way to do it on all occasions.

Undesired background color overlay while using brunch/with-react

I am not sure this issue is related directly to brunch, but it's the first time I use this framework and first time I see something like this ...
I am trying out brunch/with-react skeleton and there is a little issue with my DOM elements.
I changed my body's background color as follow, in app/styles/application.css
body {
background-color: #cfcfcf
}
Here is the result
DOM rendering
I am using Chrome so I went ahead and inspected every elements.
I cannot find any element responsible for this behavior.
Your html pages says:
<link rel="stylesheet" type="text/css" href="/app.css">
But your css filename is application.css

Line break on mobile phone only

I have a phone number on a website. It looks good on a laptop, but on a mobile device half of the number jumps to the next line. It doesn't look good.
So how can I create a line break that will only work on a mobile device sized screen?
I am not very experienced in coding, so please be specific :) Thanks a lot for any help!
Why not just do a line break as a class in the tag?
<br class="mobile-break">
and in CSS
#media screen and (min-width: 600px) {
.mobile-break { display: none; }
}
If you use Bootstrap 4 just use the break like this:
<br class="d-md-none">
You could look into the CSS word-break property to prevent words/strings being cut in half. If it is specifically line breaks you want to use then appending a class to the element such as <br class="br-on-mobile"> and setting it to display: none in the CSS should prevent it from doing anything normally.
You can then use a media query to display the line break at specific mobile screen sizes, for example:
.br-on-mobile {
display: none;
}
#media screen and (<Your conditions here>) {
.br-on-mobile {
display: static;
}
}
EDIT: static is invalid value for display. Using inherit should fix the issue. See this Fiddle
EDIT: The header of your page must also have <meta name="viewport" content="width=device-width, initial-scale=1"> to allow for correct scaling/application of media queries.
You could also achieve this by wrapping the number in a span element and setting this to display: block when on mobile devices, although your issue with the media queries below will also apply to this.
If you're looking into this in the future for something that works in Tailwind CSS, I found this to mirror the Bootstrap and BULMA style implementations (choose your breakpoint between sm/md/etc):
<br class="md:hidden">
Instead of space between the number, add this is non-breaking space which will prevent a new line being added.
This should work on BULMA: <br class="is-hidden-desktop" />

Why won't my viewport tag properly use device width (no zoom) on most mobile devices?

UPDATE 12/2020: Seeing as this post still gets activity, I must stress that there are no answers here that apply to modern web development. In fact, almost all of the answers here would put your website in a precarious situation when it comes to accommodating those with disabilities. The only thing your responsive website should include in the head tag is:
<meta name="viewport" content="width=device-width, initial-scale=1">
Much more than just UX is at risk if you attempt to lock users into a specific scale (such as lawsuits or even government fines).
-- CONTINUE AT YOUR OWN RISK --
UPDATE 03/2019: This Q&A still gets some activity, nearly 5 years my question came up. Please note this problem was due in part to more common irregularities in older mobile devices at THAT time. With today's browsers and devices, fiddling with viewport scalability would be a shoe-horn fix to a bigger problem which is likely a problem in either your CSS or possibly your markup.
I've built a dozen responsive sites and have never experienced this problem. Basically, I'm using the meta tag for viewport with width=device-width, but iPhone and Android devices are still zooming. For some reason, I don't have this problem on Windows phones.
Here is an excerpt from the head html:
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Here is the HTML for the main wrappers (note the outermost div is added from jquery.mobile:
<body class="html front logged-in no-sidebars page-node mobile-detect-class ismobiledevice" >
<div data-role="page" data-url="/?mobile_switch=mobile" tabindex="0" class="ui-page ui-body-c ui-page-active" style="min-height: 568px;">
<div class="container">
</div>
</div>
</body>
And here is the main wrapper CSS:
html,body { margin: 0; padding: 0; background-color: #d5d5d5; font-family: 'HelveticaNue', Arial; }
body {background: transparent none no-repeat 50% 0; min-height:100%; height:auto; background-size: auto 100%; width:auto;}
body > div {width: 100%; height: auto; }
.container { background: #fff none no-repeat 50% 0; margin-bottom: 20px; width:100%; position:relative;}
Bootstrap is also being loaded prior to the stylesheet.
I have tried a number of different things already including:
Removing jquery.mobile
Removing bootstrap
Updating bootstrap to the latest version
Changing the viewport tag to the following:
width=device-width, initial-scale=1.0
width=device-width, initial-scale=1.0, user-scalable=no
width=device-width, initial-scale=1.0, user-scalable=0
width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0
Adjusting various CSS properties related to width/max-width
I am completely out of ideas and seem to have exhausted anything new I can find / try via google. I would greatly appreciate any help you can provide!
After much testing and continued searching, I came up with a fix that seems to be very effective. Another solution I saw to a similar problem proposed the following:
<meta name="viewport" content="width=640, initial-scale=.5, user-scalable=no" />
This, at first, seemed like a decent solution as it worked on most phones. It bugged me a little bit though because it obviously isn't geared specifically towards working with any device width. That answer can be found here:
Android viewport setting "user-scalable=no" breaks width / zoom level of viewport
This solution worked on most mobile devices, but on some it was ineffective causing the display to be using incorrect zoom.
I believe the reason why the solution above is not effective is because not all mobile devices use a base 320 for zoom. So when device-width isn't working, this causes the constraints to be inconsistent.
I tried a bunch of different things, but then ultimately tried the following which (so far) seems highly effective:
<meta name="viewport" content="width=device-width, initial-scale=0.5, user-scalable=no">
I believe the difference in this tag is that it tells the device to use it's native dimensions, but then adds a scale factor that tells it to not zoom in as much as it would normally.
I never was able to find any CSS adjustments that solved the problem, and I welcome any other answers in the future that may add further clarification to the problem/solution.
One other thing I'd like to add, is that an alternative meta tag that may be more effective for larger devices (tablets, which the site in this problem was not designed for), may be to set maximum scale instead of using user-scalable. Like this:
<meta name="viewport" content="width=device-width, initial-scale=0.5, maximum-scale=0.5">
EDIT / Update 2018:
This question still gets quite a few hits even though it's a very dated problem. Certainly in hindsight and after much more experience with responsive, I can say that both the initial problem and the solutions were based around stop-gap issues that resulted from poorly constructed responsive CSS and HTML.
While the answers here may help future visitors solve for issues when it comes to retro-fitting more dated code, I would highly recommend that any new development conform to the latest in responsive standards. If your markup and CSS conforms, there are very few reasons why you would ever not want the standard meta for viewport in responsive (below):
<meta name="viewport" content="width=device-width, initial-scale=1">
I just removed initial-scale=1 and suddenly Android on Chrome and Silk on a Kindle both worked perfectly, site width matched to screen width in both orientations.
<meta name='viewport' content='width=device-width'>
I haven't tested on IOS, but I'll add an IOS specific hack if I need to.
I had an issue with bootstrap 4.3.1 looking like a scaled down version of a tablet view on mobile using this meta tag setup:
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
so then i used the meta tag below to fix on mobile:
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
now it looks like bootstrap 4 should look cross device
Great Stuff, however, I have discovered that if you are on a desktop and the viewport is set to a static width the backgrounds may not render on the portions of the screen not shown when you horizontal scroll.
To fix this for non-responsive pages simply add a min-width to the body.
body {
min-width: 960px;
}
Note, this is in addition to setting the viewport a static width
<meta name=viewport content="width=960">
Anyone looking for a fix in the future, stay with content="width=device-width, initial-scale=0.5" BUT adjust the initial-scale= until it works. I built a site that worked fine on different resolution monitors but was absolutely horrid on mobile. Try 0.5 then scale downwards .1 at a time until it works. On two Android phones 0.3 worked perfectly for me.
Just incase some other people are having the same issue I am, my problem as of 3/2020 seemed to be how I was setting up my inputs in my #Media query.
For me the meta tag didn't really do anything.
<meta
name="viewport"
content="width=device-width, initial-scale=1.0,maximum-scale=1.0"
/>
My problem was more
#media only screen and (max-width: 500px) {
.input-example {
font-size: .9rem;
}
}
Once I changed it to :
#media only screen and (max-width: 500px) {
.input-example {
font-size: 1rem;
}
}
Everything seemed to work fine. From what I read it needs to either be 1rem or 16px.
Check this answer. In short :
<meta name="viewport" content="width=device-width, target-densityDpi=device-dpi;" />

Changing icon and title of partial trust WPF xbap

I've been fiddling with a partial trust XBAP - how can I change the icon shown in the IE-tab and the title (aside from changing the assembly-name in the project properties)?
I would also like to change what is shown in the Internet header (right now it shows the address of the XBAP.
I had a similar issue where I could not change the title in IE 10 (this was the first stack overflow answer that comes up when you Google "xbap title"). I found the solution here:
In code, set Application.Current.MainWindow.Title to the title you want to show. This worked for me, hopefully this will be helpful for the next guy.
Load your XBAP from a HTML page using an IFRAME. In the HTML page add a title and icon.
Something like this:
<html>
<head>
<title>This is my title</title>
<link rel="shortcut icon" href="http://wherever.com/icon.ico">
</head>
<body>
<iframe location="something.xbap"
style="width:100%; height:100%; border:0; overflow:auto" scrolling="no"></iframe>
</body>
</html>
This is simplified from code that I actually use, except due to some other requirements I'm giving the iframe an id and setting its location using JavaScript in the body's onload event. I assume the above will work just as well.

Resources