Loading a web page in Android App - java

I don't know how to access my index.html webpage when the app is loaded. Actually on my code I use file:///android_asset/index.html, I've tried to load a Google page to test, it works, but it launch the Chrome app...
Here is my code of my MainActivity.java :
package com.totominc.fr.webapp;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class MainActivity extends ActionBarActivity {
#Override
protected 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/index.html"); // error may be here...
}
}
But the error which I got is that it can't find the file index.html.
Here is a screenshot of my project.
Thanks for helping me!

try this
file:///Android_res/drawable/index.html
As your index html is in drawable folder. this would work

You need to put your index.html file into your assets folder, and not /res/drawable.
So your index.html should be in /assets/index.html.

Create your assets folder in src/main/assets/ and put your index.html there, not inside your drawables. Regarding the page opening in Chrome, try this instead
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
// Changed here
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("file:///android_asset/index.html"); // error may be here...
}

Related

Webview doesnt load my page

I'm a beginner on this kinda stuff.
I have a button that when I click on it, it loads a page on webview.
The code is the following:
Right now when I'm clicking on the button, the page doesn't load.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
public class WebActivity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web2);
String url = "http://giae.aeourem.pt";
WebView web = (WebView) findViewById(R.id.webview2);
web.loadUrl(url);
}
}

Android Studio variable cannot be found WebView

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;
import static com.example.tencho.cst.R.id.*;
public class MainActivity extends AppCompatActivity {
private WebView MywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MywebView =(WebView)this.findViewById(WebView);
WebSettings webSettings=MywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
MywebView.loadUrl("www.cst.edu.bt");
}
}
This is the MainActivity.java. it's always giving me an error cannot find symbol variable WebView.
I did read the past similar post but I couldn't solve it error. Please need help!
At this line
MywebView =(WebView)this.findViewById(id);
id should be like R.id.webview1
webview1 is the id in your XML layout
The error is on this line:
MywebView =(WebView)this.findViewById(WebView);
The cause of this error is that the generated R.id class has no field named WebView (the argument you are passing to the findViewById() method). This is probably happening because your layout's <WebView> tag is missing an id attribute. Add this line to your <WebView> tag:
android:id="#+id/WebView"
Ben's answer is correct. In addition, if your new to Android, if you added this part in your tag which can be found in your activity_main.xml layout..
android:id="#+id/webview"
then in your MainActivity.java, it should be like this:
MywebView =(WebView) findViewById(R.id.webview);

Error: "error:cannot resolve symbol 'WebViewClient'

I already tried referring both to this StackOverFlow question as well as this tutorial but still have not found a solution to my problem.
I set permissions already in the Android Manifest XML file, and have my webview problem under the FullscreenActivity file. My code is here:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
WebView view = (WebView) this.findViewById(R.id.webView);
view.setWebViewClient(new WebViewClient());
view.loadUrl("platinumskincare.com");
view.getSettings().setJavaScriptEnabled(true);
mVisible = true;
mControlsView = findViewById(R.id.fullscreen_content_controls);
mContentView = findViewById(R.id.fullscreen_content);
The error I keep getting says 'Cannot Resolve Symbol 'WebViewClient', and I believe it is on line 44 of my code. What should I do and how do I fix this problem?
Check if you have these imported
import android.webkit.WebView;
import android.webkit.WebViewClient;
Clean and rebuild your project in Android Studio.

WebView opens static version of website

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
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.web_view);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new CustomWebViewClient());
mWebView.loadUrl("http://groupedirectouest.com");
}
}
class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
when I use ua (userAgent) string, webview shows desktop version of website.
here is the ua string i have used :
mWebView.getSettings().setUserAgentString("AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30");
here is the website that i am trying to open in my
http://groupedirectouest.com/
it works fine with chrome but webview.
Now there are custom chrome tabs for you!
The WebView is good solution if you are hosting your own content inside your app. If your app directs people to URLs outside your domain,it is now recommended that you use Chrome custom tabs for various benefits.Follow the link below:
https://developer.chrome.com/multidevice/android/customtabs

webView.loadUrl is not working

This is my code:
package sai.datla.game;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class GametestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.clearSslPreferences();
webView.loadUrl("file:///android_asset/www/index.html");
}
}
but it is saying that it cannot find the link in the emulator. I have checked many times if my index.html file is in the www folder, which is in my assets, but it won't work. Please help.
By the way I am only twelve years old so please make the answers easy to understand for a child.
// Find view in layout
WebView wv = (WebView) findViewById(R.id.webView_tobe_loaded);
// Get settings
WebSettings wbset = wv.getSettings();
// Enable JavaScript
wbset.setJavaScriptEnabled(true);
// Set new client (to handle website in your app)
wv.setWebViewClient(new MyWebViewClient());
// Example of the URL
String url = "http://www.google.com";
// Load website
wv.loadUrl(url);
This code will help you to solve the problem.
It worked for me.
instead of loadUrl, try using the loadDataWithBaseURL method:
webview.loadDataWithBaseURL("android.resource://<package_name>/assets/www/file_name", html, mimeType, encoding, "");

Categories