I'm trying to load a html file in my webview using this codes. but is show Webpage not available
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.frux.web.R.layout.activity_main);
WebView web = (WebView) findViewById(R.id.web1);
web.loadUrl("file:///android_assets/www/webpage");
}
}
I already edit my codes into this but still webpage not available
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView web = (WebView) findViewById(R.id.web1);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("file://android_assets/www/index.html");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
I just mistype the assets. It should be "asset" without "S".
The file should be extension with html
The easiest way would probably be to put your web resources into the assets folder then call webView.loadUrl("file:///android_asset/www/filename.html");
try this will help you...
web.loadUrl("file:///android_assets/www/webpage");
webpage is a folder or a file? I guess it should be
web.loadUrl("file:///android_assets/www/webpage/file.html");
or
web.loadUrl("file:///android_assets/www/webpage.html");
Write below line
setContentView(R.layout.activity_main);
web.loadUrl("file:///android_assets/www/webpage/index.html");
instead of
setContentView(com.frux.web.R.layout.activity_main);
web.loadUrl("file:///android_assets/www/webpage");
Related
I have a webview in my android app, the html file which gets loaded is on a server, however I need to address some files from app's assets folder. I wanted to ask how can I address those files.
Thanks
public class ViewWeb extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView wv;
wv = (WebView) findViewById(R.id.webView1);
wv.loadUrl("file:///android_asset/aboutcertified.html"); // now it will not fail here
}
}
And in general you can use
getAssets().open("filename.txt"))
To get filea from assets
On a side note:please put some code next time that you have triedget
Trouble in loading local html file from asset into a webview. HTML page has javascript code, so I have used webview.setJavaScriptEnabled(true) to enable it. It loads the html page, but not all the widgets properly. Any suggestions...
public class ViewWeb extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView wv;
wv = (WebView) findViewById(R.id.webView1);
wv.loadUrl("file:///android_asset/hello.html");
}
}
How can we get info from a webpage(link on it that downloads an excel file when you click on it)? What subjects should be searched in order to do that on Android? Thanks.
AFAIK, you will not able to know what are the link is for. There is a way to know about the link of the webview on which user clicks and it is WebViewClient.
Here is a code using which you can know that the clicked link has XLS file or pdf file or others. You can also block the link using that code
public class TestingActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView view = new WebView(getApplicationContext());
view.setWebViewClient(new MwebViewClient());
}
class MwebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains(".xls")) {
Log.d("Blah", "This link contains XLS file");
}
return super.shouldOverrideUrlLoading(view, url);
}
}
}
hello all i am very new to phonegap.. i want to webview into my application how can i add this? i've created same app. using android but how it can devleope using PHONEGAP?
private WebView mWebView;
//bla bla bla..
#Override
public void onCreate(Bundle savedInstanceState) {
mWebView = (WebView) findViewById(R.id.webviewHelp);
WebSettings webSettings = mWebView.getSettings();
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new MyjavascriptInterface(), "HTMLOUT");
mWebView.loadUrl(strURL);
mWebView.setWebViewClient(new HelloWebViewClient());
}
public class MyjavascriptInterface {
public void showHTML(String html)
{
bla bla bla...
}
}
public class HelloWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
bla bla bla...
}
}
}
thanks in advance :Pragna
For your most basic PhoneGap app you should extend your main activity from DroidGap.
import com.phonegap.DroidGap;
public class Main extends DroidGap {
/** Called when the activity is first created. */
//private Button m_button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
}
}
I would suggest checking out this link for a series of tutorials using Phonegap within the Eclipse development environment. Once you are setup on eclipse, you only need to overide a few lines of code in the Android Activity class to call your web pages. Phonegap takes care of the rest in terms of opening up a webview class and rendering your html within it. You will no longer to code that yourself (as you've done above). The tutorials spell it out quite clearly. You can also add your own custom JavaScript interface methods as well. Again that's described in the tutorials. Hope this helps.
In your case there is a need to Embedding Cordova WebView on Android here is link for it
Modify your main Activity as
public class MainActivity extends Activity implements CordovaInterface {
CordovaWebView cwv;
/* Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cwv = (CordovaWebView) findViewById(R.id.tutorialView);
cwv.loadUrl("file:///android_asset/www/index.html");
}
And in your layout replace your webview with
<org.apache.cordova.CordovaWebView
android:id="#+id/tutorialView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
I am new to Android and java development. I create a WebView object in the OnCreate function. Now I need to be able to pass this same object to other functions in the code, such as the onOptionsItemSelected function. I currently have it where I just create a new WebView object in each function where I need it, but this slows down the code since it has to recreate it and such.
Something as simple as this?
public class MyActivity extends Activity {
WebView wv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wv = new WebView(this);
setContentView(wv);
#Override
public boolean onOptionsItemSelected(MenuItem item){
wv.doSomething();
}