I have a simple android application, an activity with a WebView as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gfx);
WebView browser = (WebView)findViewById(R.id.webView1);
browser.loadUrl("http://www.somepage.com");
}
My internet page is being changed by some ajax actions, first it shows a progress bar then it redirects to another page.
What happens to me that my browser stays in the first page, and doesn't respond to the changes in the page, it doesn't act like a real browser.
If I open the page directly in the browser using:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.somepage.com"));
startActivity(browserIntent);
it works fine.
Related
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);
}
I have a WebView in my app with javascript enabled:
textView=(TextView) findViewById(R.id.output);
webView=(WebView) findViewById(R.id.browser_main);
//Tried with those settings,nothing changed
/*webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);*/
webView.addJavascriptInterface(new JSInterface(this), "interface");
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
webView.loadUrl("javascript:window.interface.receiveRaw(document.getElementsByTagName('html')[0].innerHTML);");
}
});
The URL I load returns just some text that's being updated by javascript with setTimeout();. However, after some lines of text WebView stops rendering the page. If I tap the screen the full page with the full text is shown.
For example, if the output of the page is
"Hello\nStack\nOverflow\nHow\nAre\nYou?"
I've got this when I start the app:
Hello
Stack
If I tap on the screen, the full text gets shown:
Hello
Stack
Overflow
How
Are
You?
The problem is that i need the full output when the app starts.
Is there any setting i can change?
May WebChromeClient solve the problem?
I am trying to load this popular podcast in an Android WebView:
http://www.stitcher.com/podcast/entrepreneuronfirecom/entrepreneur-on-fire-tim-ferriss-other-incredible-entrepreneurs
And this is how I render it:
public class PodcastsActivity extends BaseActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
WebView webview = new WebView(this);
webview.getSettings().setAppCacheEnabled(false);
webview.getSettings().setJavaScriptEnabled(true);
webview.setInitialScale(100);
webview.setWebViewClient(new WebViewClient());
setContentView(webview);
webview.loadUrl("http://www.stitcher.com/podcast/entrepreneuronfirecom/entrepreneur-on-fire-tim-ferriss-other-incredible-entrepreneurs");
And in my manifest I have the activity defined like this:
<activity
android:name="PodcastsActivity"
android:label="Podcasts"
android:hardwareAccelerated="true"/>
But it renders like this:
Is there an extra setting that needs to be set? Or something I am missing?
Thanks!
I hope you have looked at other answers. Some of the things you may try are:
1) From webview not loading correctly in application question, you may try and enable java script before loading the URL:
webview.setInitialScale(1);
webview.getSettings().setAppCacheEnabled(false);
webview.getSettings().setJavaScriptEnabled(true);
2) As per Android webview not rendering html content correctly question, if your target is higher than 2.3.3 try adding this in your manifest file.
android:hardwareAccelerated="true"
Update
3) Also check that you have following permission in manifest as direct child to <manifest> tag:
<uses-permission android:name="android.permission.INTERNET" />
Unable to load webpage using WebView in Android
4) You can try shouldOverrideUrlLoading() method as stated here.
Update 2:
As you said that it displays correctly on your browser, there is another possibility of using the above method in case when a certain link is clicked within the app, it opens the default browser. I'm not sure if you would want this but it is a possibility. Something as follows:
webview.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
});
webview.loadUrl("http://www.stitcher.com/podcast/entrepreneuronfirecom/entrepreneur-on-fire-tim-ferriss-other-incredible-entrepreneurs");
Source: WebView link click open default browser
Hope this helps.
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.
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.