Load URL Within App - android

I am looking for a way to load an external website within a page in the app.
For example have a website link for a company on the main screen and when clicked have the transition effect to another page that displays the company website, but also allows you to include the header and footer for the page. You can also include options to go back, or other links.
I believe this is possible, so any help with this would be great. Thanks,

I believe you are talking about Android's WebView class.
http://developer.android.com/reference/android/webkit/WebView.html
A View that displays web pages. This class is the basis upon which you can roll your own web browser or simply display some online content within your Activity. It uses the WebKit rendering engine to display web pages and includes methods to navigate forward and backward through a history, zoom in and out, perform text searches and more.

http://developer.android.com/resources/tutorials/views/hello-webview.html
You just need to use the webview view. After that it's pretty simple to place the view wherever you want and send it to any url you'd like.
WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
}

I currently am using this method and it works.
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class ThirdActivity extends Activity
{
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.html_view);
webview = (WebView)findViewById(R.id.viewHTML);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("https://twitter.com/LaserPros");
}
}
don't forget your xml file mine is named html_view it has a webview in it with the id of viewHTML the file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/viewHTML"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
in the class activity the important thing to (and what most people miss when giving an answer) is to set your web view as the WebClient otherwise your phone will want to open the url in a external browser.

Related

file://android_asset/www/index.html was not found

I am new to android programming and I am developing my first apps using eclipse.
I have kept my html and jquery codes inside assets folder.
now this is my code in mainactivity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView)findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
// webView.setWebChromeClient(new WebChromeClient);
webView.loadUrl("file://android_asset/www/index.html");
}
when i installed app on my device and run it,
it says,
webpage not available,
the file at file://android_asset/www/index.html may have been moved permanently
plz help me to solve this
Try changing
webView.loadUrl("file://android_asset/www/index.html");
by
webView.loadUrl("file:///android_asset/www/index.html"); // please see the extra forward slash, they have to be 3.

Get latest content from websites in android app

I want to get the content to be displayed in my android application as soon as it is updated on a particular website.
how can I do that? I'm new to android development....please help
sure use WebView - Android documentation here: http://developer.android.com/reference/android/webkit/WebView.html
You can also consider using PhoneGap and make a hybrid native html5 app.
Hi you can use WebView for this purpose please have alook as the example is given below
res/layout/main.xml –
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
the WebViewActivity.java is as follows
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class WebViewActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
}
}
do not forget to have permission at manifest
<uses-permission android:name="android.permission.INTERNET" />
You can display the web content using these ways,
Through cross platforms, such as PhoneGap with jQuery Mobile or Sencha touch etc,.
Through Android WebView class.
If you try to display a web content using Android WebView class, refer this sample tutorial.

Adding local css/js files from asset folder to remote web page

I have an Android webView which loads a remote web page. As the code of this page is controlled by a colleague I can ask him to change it but I would prefer to find a solotion totally on client side.
For reasons regarding traffic and performance we want to store our css and javascript files locally on the client and not load them from the server.
What I came up with are two ideas but none of them worked out so far.
Store all the files in the asset folder and have the html refer to them
Problem: The webview seems to be not allowed to access "file:///..." urls
Question: Is there a way to work around this issue?
Just ignore all those references in the html and just inject all those files after loading them in the webview
Question: Just how do I add those files (.css / .js) to my already loaded html?
You can build a local WebView in the following way
The Activity (LocalWebviewActivity.java)
The Layout (activity_localwebview.xml)
The Assets folder (in the root of "assets" folder, create the folder "css" and place "style.css" in here)
You refer to JS files the same way you refer to CSS StyleSheets
LocalWebviewActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class LocalWebviewActivity extends Activity {
WebView myWebView;
StringBuilder mySBcontent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_localwebview);
myWebView = (WebView) findViewById(R.id.webkit);
mySBcontent = new StringBuilder();
mySBcontent.append("<html>");
mySBcontent.append("<head>");
mySBcontent.append("<link type='text/css' rel='stylesheet' href='css/style.css'>");
mySBcontent.append("</head>");
mySBcontent.append("<body>");
mySBcontent.append("<h1>My Heading</h1>");
mySBcontent.append("<p>My HTML content</p>");
mySBcontent.append("<p><img style='width:150px;' src='myImg.png' /></p>");
mySBcontent.append("</body>");
mySBcontent.append("</html>");
myWebView.loadDataWithBaseURL("file:///android_asset/", mySBcontent.toString(), "text/html", "UTF-8", "");
}
}
activity_localwebview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="#+id/webkit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>

How to display Web site in Android WebView

I am new to Android WebView, in my application i need to display a web site conatins 3 web pages. In the first web page there will be a link to navigate to the second page and second to third page. I given the URL in the WebView, the first page is displayed perfectly, when i click the link it directly opens the browser application to display the second page. But i want to display the second page in the WebView itself. Please find my code below:
WebView forumView=(WebView)findViewById(R.id.forumView);
forumView.getSettings().setJavaScriptEnabled(true);
forumView.loadUrl("url");
As i said i am very new to WebView my code might be wrong, please help me to solve this problem.
Thanks in Advance,
Rajapandian
This piece of code will help you.
wbb = (WebView) findViewById(R.id.webView_tobe_loaded);
WebSettings wbset=wbb.getSettings();
wbset.setJavaScriptEnabled(true);
wbb.setWebViewClient(new MyWebViewClient());
String url="http://www.google.com";
System.out.println(getdeviceid());
wbb.getSettings().setJavaScriptEnabled(true);
wbb.loadUrl(url);
You'll have to intercept the clicks yourself if you don't want the default Android behavior.
You can monitor events in a WebView using a WebViewClient. The method you want is shouldOverrideUrlLoading(). This allows you to perform your own action when a particular URL is selected.
You set the WebViewClient of your WebView using the setWebViewClient() method.
If you look at the WebView sample in the SDK there's an example which does just what you want. It's as simple as:
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}

Load Web Page Into my Activity

OK. Have been trying for two weeks to get a simple web page I created in FrontPage (test.htm) to load into my Activity in my Eclipse Emulator. I have placed my page (test.htm) into my 'assets' folder and created the VERY simple code below. I STILL cannot get the page to load into my Emulator. I am using an XML with WebView within a Linear Layout (myxmlfile). Does anybody see anything blatantly wrong?? I have also tried: file:///asset/test.htm but my assets folder is asset(s) with an 's'.
public class Activity5 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myxmlfile);
WebView x = (WebView) findViewById(R.id.webview);
x.loadUrl("file:///assets/test.htm");
}
}
Please HELP!
Do you have to add this uses permission
<uses-permission android:name="android.permission.INTERNET"/>
into your AndroidManifest.xml ?
I think your html path is not correct. It must be android_asset instead of asset:
x.loadUrl("file:///android_asset/test.htm");

Categories

Resources