I have this code:
Button bluehost = (Button)findViewById(R.id.bluehost);
bluehost.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.bluehost.com/track/businessplan/"));
startActivity(browserIntent);
}
});
Technically, this code works to send the person to that url, but for some reason this particular URL goes to a page not found from this code, but renders if you put this url into a browser. But when done from the app it goes to page not found. I am testing with Android Safari browser.
Is it because of the redirect on the URL? And how could I fix this?
I do have this line in my manifest file:
<uses-permission android:name="android.permission.INTERNET" />
NOTE:
Internet connection is NOT the problem. This code works for other URLs. It is something strange that is happening with this specific link.
Thanks!
The code in my answer below and the corresponding redirect worked for me.
On my Xperia Z Ultra, it looks like this:
According to the official Android documentation, it should also work on previous api level below 19. I assumed the Chrome documentation mentioned the minimum api level number 19 because it was specifically targeting web developers that wanted to use its HTML5 functionality.
That being said, this solution does not work with my old Sprint XPRT (Android 2.3.5 - API Level 10) and it does not work on an emulated Nexus S (with an old image of KitKat on it). It says "web page not found" on both. And when I further tried it on my XPRT using the browser, but entered the url manually, I get the dialog that "A secure connection could not be established". Unfortunately, removing the 's' in https doesn't work either, because it redirects to the secure version, and I still get "A secure connection could not be established".
Earlier Answer:
If you follow that Chrome WebView tutorial exactly. You'll get to this part. The emphasis in bold is mine.
Handling Navigation
Now try changing the URL you're loading to http://www.html5rocks.com/ and rerun your application. You'll notice something strange.
If you run the application now with a site that has a redirect like html5rocks.com, your app ends up opening the site in a browser on the device, not in your WebView -- probably not what you expected. This is because of the way the WebView handles navigation events.
Here's the sequence of events:
The WebView tries to load the original URL from the remote server, and gets a redirect to a new URL.
The WebView checks if the system can handle a view intent for the URL, if so the system handles the URL navigation, otherwise the WebView will navigate internally (i.e. the user has no browser installed on their device).
The system picks the user's preferred application for handling an http:// URL scheme -- that is, the user's default browser. If you have more than one browser installed, you may see a dialog at this point.
In other words, if you just stop the tutorial at this point, you'll get the behavior you desire.
Here is the actual code in case the link to that tutorial stops working:
MainActivity.java
public class MainActivity extends Activity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.loadUrl("https://www.bluehost.com/track/businessplan/");
}
@Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
}
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
tools:ignore="MergeRootFrame">
<WebView
android:id="@+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Also for anyone else reading this, I am not mentioning the needed INTERNET permission in the manifest because the original person asking already has that part covered.