Import like this
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebSettings;
import android.webkit.WebViewClient;
and then:
WebView myWebView = (WebView) findViewById(R.id.web1);
myWebView.loadUrl("http://www.example.com");
But the it shows errors for line "myWebView.loadUrl("http://www.example.com");"
Multiple markers at this line
- Syntax error on token ""http://google.com"", delete
this token
- Syntax error on token(s), misplaced construct(s)
What's wrong with me?
your site will never load in your app, it will always ask to open with a external browser because you are missing some code. The following will work.
setContentView(R.layout.nameof_your_webview_xml_file_here);
WebView myWebView = (WebView) findViewById(R.id.web1);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("http://www.example.com");
you forgot to set your view as the web client. also if the page uses javascript i think you will need:
myWebView.getSettings().setJavaScriptEnabled(true);
put that line above the line where you setWebClient. This should work.. it is working for me. I use it to load my companies url, Facebook and twitter sites.
You can make use of the following code:
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
// Load URL
mWebView.loadUrl("http://www.firstdroid.com/advertisement.htm");
Also, make sure your manifest file has following permission:
<uses-permission android:name="android.permission.INTERNET"/>
Try by passing URL/String variable.
Related
I have been developing an android app with games created from flash.
I have researched and found out that it can be possible to integrate the swf file in the android by copying it to the assets folder and placing the url path in a webview. I tried it and no errors were found except when I run it to the emulator, the output was just a plain white screen.
here is the code I used:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class SWFFileActivity extends Activity {
private WebView mWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swffile);
mWebView = (WebView)findViewById(R.id.webview);
mWebView.getSettings().setPluginState(PluginState.ON);
mWebView.loadUrl("file:///android_asset/fingerspell");
}
}
when I try to put the .swf extention in the url, "Web page not found" message displays on the white screen.
my project's SDK is 18.
I can't figure out what the problem is.
Is there any other way to integrate swf files in an android project?
Is this really possible?
So I have a website with mobile version. I am trying to do an App for it.
This is my activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"/>
and this is my MainActivity.java class
package com.example.esouqbh.esouq;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.example.esouqbh.esouq.R;
public class MainActivity extends Activity
{#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar as we already have it in the web app
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Point to the content view defined in XML
setContentView(R.layout.activity_main);
//Configure the webview setup in the xml layout
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
//Yes, we want javascript, pls.
webSettings.setJavaScriptEnabled(true);
//Make sure links in the webview is handled by the webview and not sent to a full browser
myWebView.setWebViewClient(new WebViewClient());
//And let the fun begin
myWebView.loadUrl("http://esouqbh.com"); }}
for some reason when I run the app it says on my phone (Im running the APP on my phone as an emulator)
I get this error
Web page not available
The web page at http://esouqbh.com could not be loaded as:
net::ERR_CACHE_MISS
note that my website functions perfectly with no problem if I open it from my computer browser or phone.
EDIT:: Found solution by adding
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
in androidmanifest.xml!
NO, it´s not a folder, by adding :
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
into you AndroidManifest.xml means that your application will load the page from the internet into your WebView
INTERNET permission: allows applications to open network sockets.
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.
Can I call .java file from .HTML file, with the help of link in android?
I am new to android application development. And I have not that much idea. So Please Help me.
Location of .java file is in src/my_package_name folder and location of .HTML file is in assets/www folder.
I am trying to do with this by anchor tag in html file, I have given href="ContactUs.java" to link. It is not working.
Emulator shows me error. That is Application Error. The Requested file was not found. ContactUs.java(file:///android_asset/ContactUs.java).
I am not sure it is correct way or not.
So please Suggest me if there is any better way to do this?
Thanks in Advance..
Here is a solution to open an activity from html.
First your main activity containing your webview:
package com.test.jsitest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class WVActivity extends Activity {
private WebView m_wv = null;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wv);
m_wv = (WebView) findViewById(R.id.webview);
m_wv.getSettings().setJavaScriptEnabled(true);
m_wv.loadUrl("file:///android_asset/www/index.html");
m_wv.addJavascriptInterface(new ActivityLauncher(this), "Android");
}
}
Then your Javascript Interface:
package com.test.jsitest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
public class ActivityLauncher {
private Context m_context;
public ActivityLauncher(Context context) {
m_context = context;
}
public void launchActivity() {
m_context.startActivity(new Intent((Activity)m_context,
Activity2.class)); // Here you replace by your activity (ContactUs)
}
}
And finaly your html file:
<DOCTYPE html>
<html>
<head>
</head>
<body>
Link
</body>
</html>
I tested it and it works under a level 8 API.
I hope it helps you.
I think you're looking for something like this: http://developer.android.com/guide/webapps/webview.html#BindingJavaScript
You can't do it via html but you can via javascript, using a JavaScript Interface.
You can not call java file at all. Java file on android is most probably part of application ( and thus activity ) - usually you have to use some URL protocol, and androis system will call suitable application for you
Here is more about it:
How to call android application from javascript
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.