I am working on an application with many html pages. I have created these in the raw folder on Eclipse.
I'm going to be making many html files and I want to have only one location for the css file which I want to call in each of the html files. This application works in such a way that the
loadData method displays the html page as shown below:
webview.loadData(readTextFromResource(R.raw.gen1), "text/html", "utf-8");
Id appreciate any ideas.
you can copy html and css files in your project assets and load html file from asset to webview like below code:
WebView wv;
wv = (WebView) findViewById(R.id.webView1);
wv.setBackgroundColor(0);
wv.setBackgroundResource(android.R.color.black);
wv.setWebChromeClient(new WebChromeClient());
wv.setWebViewClient(new WebViewClient());
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setPluginsEnabled(true);
wv.loadUrl("file:///android_asset/Index_Animation/test.html");
like below screen shot:
and refer css in html file below:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="./style.css" type="text/css" media="screen"><!--This line refer the css file-->
</head>
<body bgcolor="#000000">
<div class="col_left">
<div class="logo">
<div class="circles">
<div class="circle"></div>
<div class="circle1"></div>
<div class="circle2"></div>
</div>
<center>
<img src="./Untitled.png" />
</center>
</div>
</div>
</body></html>
Related
I'm trying to load html with CSS which has ID selector and the file doesn't load to the webview as expected.
This is the file which I tried to load:
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: red;
}
</style>
</head>
<body>
<p id="para1">Hello World!</p>
<p>This paragraph is not affected by the style.</p>
</body>
</html>
However when I tried the code below,it loads to the webview.
<html>
<head>
<style>
h1 {color:red;}
p {color:blue;}
</style>
</head>
<body>
<h1>A heading</h1>
<p>A paragraph.</p>
</body>
</html>
I have enabled these properties on the WebView:
mWebView.setWebViewClient(new CustomWebViewClient());
mWebView.setWebChromeClient(new CustomWebChromeClient());
mWebView.getSettings().setSupportZoom(true); // ZoomControls
mWebView.getSettings().setBuiltInZoomControls(true); // Multitouch
mWebView.getSettings().setDisplayZoomControls(false);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(CONFIG.webviewEnableViewport());
mWebView.getSettings().setAllowFileAccess(true);
mWebView.requestFocus(View.FOCUS_DOWN);
mWebView.setHorizontalScrollBarEnabled(true);
mWebView.setVerticalScrollBarEnabled(true);
Can anyone help me on this?
I could solve the issue by using the base64 encoding.
String encodedHtml = Base64.encodeToString(myHtmlString.getBytes(),
Base64.NO_PADDING);
mWebView.loadData(encodedHtml, "text/html", "base64");
Problem:
I should display offline a Html page stored in my react-native app, and for to do it I used WebView:
<WebView
originWhitelist={['*']}
source = {{uri:'file:///android_asset/index.html'}}
/>
but it load just html file without css and/or js, this is an example of my html page:
my file html is stored in: react-native-project->android->app->src->main->assets->web
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/styles.css"/>
</head>
<body >
<h1>Hello World</h1>
</body>
</html>
this is an example of my css page:
my file css is stored in: react-native-project->android->app->src->main->assets->web->css
h1 {
color:red;
}
I have found this post on medium but not work for my and BaseUrl not work.
Question:
How I can display a Html page with relative file css and js connected to it offline on react-native?
can I use the cache how do react-native-offline-cache-webview ?
Thanks for your attention and sorry if my English is wrong but I'm not a native speaker bye
Actually I want to load complete site from internet link but i want to load css files from mobile (means not from internet). Because I want to load all page faster using internal local css file.
Example :
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="dist/css/font-awesome.min.css" />
</head>
</html>
So Here I want to Load "bootstrap.min.css" and "font-awesome.min.css" from mobile memory only.
My Android Code :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "http://www.MySiteName.com/";
view = (WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true); // Enable JavaScript Support
view.setWebViewClient(new WebViewClient()); // Links open or Navigate in same webView not in Browser
view.loadUrl(url);
}
Please put CSS in assets folder, and refer to CSS by relative path, and load HTML to WebView by loadDataWithBaseURL() method:
if your css file name is mycss.css
StringBuilder data = new StringBuilder();
data .append("<HTML><HEAD><LINK href=\"mycss.css\" type=\"text/css\" rel=\"stylesheet\"/></HEAD><body>");
data .append(tables.toString());
data .append("</body></HTML>");
webView.loadDataWithBaseURL("file:///android_asset/", data .toString(), "text/html", "utf-8", null);
thank you
Because the two other answers are just copy pastas and I happened to have the same issue I will help you with my solution:
You need to specify the file path in the head section of your html file.
Pay attention to the part after href
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="file:///android_asset/bootstrap.min.css" />
<link rel="stylesheet" href="file:///android_asset/font-awesome.min.css" />
</head>
</html>
I just can't get my head around why the other posters just copied and pasted and didn't even read what you were trying to tell them.
I hope this helps.
Be aware that your resources folder can be named differently.
I am loading a html from the assets folder. It is positioned below an image-view. The code works, however it takes a second for the html file to appear on the device.
here is the code:
public class FgmIsChildAbuse extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView mWebView;
mWebView = (WebView) findViewById(R.id.webviewm1);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/m1_section1_read.html");
ImageView ImageView1 = (ImageView) findViewById(R.id.ImageView1);
Resources res = getResources(); /** from an Activity */
ImageView1.setImageDrawable(res.getDrawable(R.drawable.images_eyes));
mWebView.getSettings().setRenderPriority(RenderPriority.HIGH);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
}
}
Below is an example of one of the html files
<html>
<head>
<title>CFAB</title>
<meta name="viewport" content="width=300, initial-scale=1" />
<link rel="stylesheet" media="all" href="master.css" type="text/css" />
</head>
<body bgcolor=transparent>
<FONT COLOR="#000000" size = 3>
<div class="outer" data-role="page" data-theme="a">
<div class="main" data-role="content">
<div class="tab-content">
<div class="tab1 readtab">
<h3>Female Genital Mutilation (FGM) is Child Abuse</h3>
<p>Over <strong>100,000</strong> women in the UK have been <strong>victims</strong> of Female Genital Mutilation (FGM). The vast majority of these victims will have been subjected to this horrific form of abuse either <strong>before they came to the UK</strong> or they will have been taken, as children, to their parents' <strong>country of origin</strong> to undergo this abuse.</p>
<p>This awareness course is designed to help <strong>practitioners</strong> understand what FGM is as well as the context within which it takes place and the relevant legislation to combat it. Critically, it is to build <strong>confidence</strong> so that professionals are better prepared to identify those children most at risk of FGM and those who may have been victims of this type of abuse.</p>
</div>
</div>
</div>
</div>
</body>
</html>
hi friends,
i want to load swf file in android emulator.but there is problem to display.
problem is when i run the project display blank white screen. here is the code ::
String url ="file:///android_asset/co.swf";
WebView wv=(WebView) findViewById(R.id.webview);
wv.getSettings().setPluginsEnabled(true);
wv.loadUrl(url);
is there any permission are give to manifest file?
I think you won't be able to access the swf direcly from webview. So instead you have to embed the swf file in html and then call that file instead.
This both files should be placed in assets folder inside your project.
So your HTML file will look somewhat like this
File:co.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8" />
</head>
<body style="margin: 0; padding: 0">
<object id="i1">
<param name = "movie" value = "co.swf">
<embed src = "file:///android_asset/co.swf" ></embed>
</object>
<script>
var x = document.getElementById("i1")
x.setAttribute("height", screen.height);
x.setAttribute("width", screen.width);
</script>
</body>
</html>
In Java file:
setContentView(R.layout.activity_main);
WebView wv = (WebView)findViewById(R.id.webView1);
WebSettings ws = wv.getSettings();
ws.setPluginState(PluginState.ON);
ws.setJavaScriptEnabled(true);
ws.setAllowFileAccess(true);
wv.loadUrl("file:///android_asset/co.html");
And the main xml (activity) should contain a webview possibly with id webView1 which is automatically generated. This should work.