Web Page Not Available - Loading local HTML on Android - java

Yo guys, this is bugging me and I can't find a fix for it,
I have a simple activity to launch a WebView and display a HTML file
public class HelpViewer extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.help_page_layout);
WebView browser = (WebView) findViewById(R.id.help_page_webview);
browser.loadUrl("file:///android_asset/help.html/");
}}
The help_page_layout is your standard WebView resting in a LinearLayout - Not problems there.
The help.html is sitting under the /res/raw/ directory. From what I have see online, there shouldn't be a problem here either.
But when the WebView goes to load, it just tells me that the WebPage was not available because help.html couldn't be found.
What am I doing wrong?

This question suggests the file should be in /assets rather than /res/raw. You also have a trailing slash on the end of your URL.

Related

WebView in Android Studio does not show sites properly (i.e. www.google.com is without logo, search bar and all the stuff on upper screen)

I have a problem with showing simple google.com in my WebView.
Tried this on few emulators and 2 phones, all the same, something is wrong with my code.
1. Issue
First screen shows normally google's asking user to accept their legal stuff (1st image).
After accepting it moves to google's main page. But only lower part (Country, settings, adds, about etc.) is shown (2nd image). There is no Google logo, no search bar and buttons and no stuff from the upper display (like GMAIL, GRAPHICS, ACCOUNT and so on).
1ST SCREEN OF GOOGLE.COM
INCORRECTLY SHOWN GOOGLE PAGE
2. Code
I added permission in Manifest.
Trying to solve the issue I stackoverflowed below two lines and added to Manifest, but to no avail:
android:hardwareAccelerated="true"
android:usesCleartextTraffic="true"
**
3. My java code is below:**
(each of the settings I tried to comment out and then one by one uncomment, but still no result).
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = findViewById(R.id.webView);
WebSettings settings = webView.getSettings();
settings.setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webView.loadUrl("http://www.google.com");
}
}
Could someone please check this is tell me what I've done wrong here?
Stupid thing, should not have done this late into the night.
The problem was only with layout, rest of the page was out of screen.
Needed only to match the WebView to constraints.

Problem with restoring webview state after app installation

I have a simple android app with WebView which opens the site main page. I need to save WebView state when the user presses "home" or "main menu" buttons. And then, when user opens app from the homescreen or main menu I need to load this saved state.
Everything works fine except the first launch (after installation of built apk file): if I open app from main menu the WebView restores just fine, but if I try to open it from homescreen it starts to reload. Is there any ways to fix that? Thanks in advance.
P.S. if I install app from Android Studio (not from built apk file) everything works as I expect, so I am confused.
These are methods where I save and restore WebView:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
if (savedInstanceState != null)
webView.restoreState(savedInstanceState);
else
webView.loadUrl(getString(R.string.URL));
}
protected void onSaveInstanceState(Bundle outState)
{
webView.saveState(outState);
super.onSaveInstanceState(outState);
}

Adding the contents from XML to Java

I have been reading this tutorial -
http://developer.android.com/training/basics/firstapp/building-ui.html
and I got a problem... In the tutorial they explain the XML elements (TextField and Button) and in the end they say how to tun the program to see the button + the text field.
The problem is, that the emulator doesn't show them.. just a black screen.
I guess it's because I haven't added anything to the .java main file, but in the tutorial nothing was mentioned about it.
What should I do?
Thanks a lot for everyone who will help!
If your XML file name is main.xml then to display it content you need this method
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

Android webview script tag not loading source file

I'm trying to load a javascript file from a script tag in Webview, but it won't load it!
here's the source for the HTML Script tag:
<script type="text/javascript" src="file:///android_asset/game/tetris.js"></script>
And the java file:
WebView webview;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("file:///android_asset/game/tetris.html");
}
I've tried with and without the "file:///android_asset/game/" and it still won't load!
Any ideas? Thanks!
I do the same in my applications with relative paths, and it works fine for me.
In this case I'd simply use src="tetris.js" in the HTML

epub files does not downloadable [ANDROID]

I've set set files to localhost an set the href links for the epub files ..
more-utopia <br>
and I've try to browse from my custom web view .. but when I click to download the link , it appears no download .. and open the file in the browser with custom web view.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView web = (WebView) findViewById(R.id.webview);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://10.0.2.2/epub");
web.setWebViewClient(new myWebView());
}
class myWebView extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
url = "http://10.0.2.2/epub/";
view.loadUrl(url);
return true;
}
}
What I want to do is to download the files and want to set path the download location to user selected location .
Any helpful tips are humbly welcome.
Your code is set up to prevent people from downloading anything. Every time they click the link, you have them load the original Web page again. Try using setDownloadListener() on your WebView instead. Or, change your shouldOverrideUrlLoading() to do what your question says you want it to do.

Categories

Resources