My application needs to retrieve data from a web site.
I use a webview to access the required page. In the page displayed in this webview there are links leading to the detailed info that the app needs. When following a link (using the webView.loadUrl()) a popup is displayed on top of the webview.
The app gets access to the links with the following code:
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new BackgroundScriptInterface(), "HTMLOUT");
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url)
{
webView.loadUrl("javascript:window.HTMLOUT.processHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
}
});
and
public class BackgroundScriptInterface {
#JavascriptInterface
public void processHTML(String html) {
...
}
But this BackgroundScriptInterface is not called when a popup displays.
How do I get access to the html contents of the popup?
EDIT
As suggested by Eddie below, I've tried
webView.evaluateJavascript(item.competDetailUrl, new ValueCallback<String>() {
#Override
public void onReceiveValue(String s) {
Log.d("competDetail", s);
}
});
The popup is still displayed as expected. However the value passed to the onReceiveValue callback is "null".
Try using webView.evaluateJavascript() instead of webView.loadUrl(), unless you need to work with pre-Kitkat devices
Reference:
Android Calling JavaScript functions in WebView
The info the app retrieves from that site do not need to be displayed on the UI. They are parsed and used in the app logic. Hence I have designed the following work-around: I have replaced the original javascript:apex.navigation.dialog() by a javascript:apex.navigation.redirect() that displays the requested info directly into the webview instead of into a popup window.
As the info now comes into the webview, it can be accessed by the above BackgroundScriptInterface.
I have searched and searched and searched for hours on this and can't find a solution that makes any sense for my problem. I am simply trying to open a web page from inside my android application. Should be simple, but I keep getting the No Activity found error and the app crashes. My code is extremely simple for this...
AboutActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
Button appsButton = findViewById(R.id.about_button);
appsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(Keys.MARKET_LINK));
intent.setPackage(getPackageName());
startActivity(intent);
}
});
}
in my Keys class...
public static final String MARKET_LINK = "https://play.google.com/store/apps/dev?id=<MY ID>";
Every time I click the "aboutButton" in the app I get the error...
android.content.ActivityNotFoundException: No Activity found to handle
Intent { act=android.intent.action.VIEW
dat=https://play.google.com/...
Everything I have found online all say the exact same thing... Your url didn't contain the "http://" part so it failed, but you can see my url does contain the "https://" part of the url. It is a complete URL, I can type it into a browser window and the page opens up perfectly. I don't understant how it can not have an activity to handle an Intent.ACTION_VIEW. I have no idea where to go now since everything online says add "http://" to the url and it will work, but it doesn't. Also, I do have the
<uses-permission android:name="android.permission.INTERNET />
in my manifest file. Any help would be appreciated, it is driving me insane. Thank you
You are targeting a specific package with
intent.setPackage(getPackageName());
So the system is looking for an intent filter within your app to handle the intent. If you remove this it will look through other apps on the device and find browsers which should allow you to open them.
I'm working on an android app while learning to code in Java and I'm building a note taking app currently and am trying to convert what used to be a toast message to a snackbar message. Below is what I currently have along with the toast message that used to be there that is now commented out. I'm getting stuck on the method it says to call, any help would be greatly appreciated!
private void deleteNote() {
getContentResolver().delete(NotesProvider.CONTENT_URI,
noteFilter, null);
Snackbar.make(this, getString(R.string.note_deleted), Snackbar.LENGTH_LONG).show();
// Toast.makeText(this, getString(R.string.note_deleted),
// Toast.LENGTH_SHORT).show();
setResult(RESULT_OK);
finish();
}
The error android studio gives me is it cannot resolve the method "make".
The make function takes a view as the first parameter.
public static Snackbar make(View view, int resId, int duration)
If you need it in the bottom of your activity pass findViewById(android.R.id.content) as the first parameter.
I have an android application that I am developing using the emulator running on android 2.3.3 with an embedded WebView in a framelayout nested in a linearlayout (vertical). No matter what code I use it never actually does anything. I have Permissions on the android application set to INTERNET.
Even when trying to load just the HTML code. I get a blank white page, no errors no nothing. Nothing in logcat as well.
WebView wview = (WebView)findViewById(R.id.webView1);
wview.getSettings().setJavaScriptEnabled(true);
I have tried all three of these to attempt to load anything into the webview:
wview.loadUrl("http://www.google.com");
wview.loadData("<HTML><BODY><H3>Test</H3></BODY></HTML>","text/html","utf-8");
wview.loadDataWithBaseURL(null, "<HTML><BODY><H3>Test</H3></BODY></HTML>","text/html","utf-8",null);
All three give me the same results. Nothing.
Any ideas?
WebSettings settings = wbView.getSettings();
settings.setDomStorageEnabled(true);
It might be a bit too late, but I wanted to share my experience with you, because I had the same problem and spent a lot of time on it.
Put your WebView in RelativeLayout and not in FrameLayout.
What I have tested: My WebView's onPageFinished() was called every time, but on the screen I got blank page.
From chrome://inspect WebView debugger I was able to "ping" my WebView by pressing inspect button, and after that WebView was refreshing immediately.
My thoughts - WebView isn't rendered correctly in FrameLayout, and RelativeLayout fixes the issue (even invalidate() and requestLayout() calls didn't help).
I think if you edit some of your code then it will be ok.Look how i do it in my case below.Try for you in this way..
For displaying a web page
final WebView wbView = (WebView) findViewById(R.id.WebView);
WebSettings settings = wbView.getSettings();
settings.setJavaScriptEnabled(true);
wbView.loadUrl("https://play.google.com/store/apps");
wbView.clearView();
wbView.measure(100, 100);
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);
For showing HTML:
See this tutorial..
android-tutorial-html-file-in-webview
instead of passing null, you should pass an empty String to it. ex: ""
So you should call it like this:
wview.loadDataWithBaseURL("", "<HTML><BODY><H3>Test</H3></BODY></HTML>","text/html","utf-8","");
In my case, it was due to the fact that my webview was attempting to load a page using HTTPS where the certificate was untrusted.
You can check if this is the case in your code using the following snippet:
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
Java
override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
handler?.proceed()
}
Kotlin
Then, if it is the case, quickly remove the snippet and refer to the documentation to diagnose the problem further and potentially add the certificate to your keystore.
If it still Doesn't work then please make sure that you have added "http://" or "https://" before the web address.
I gave +1 because these were both valid assistance to getting the webview to working properly, but it was not the solution to my issue.
What I didn't realize is I had a wrap_content on the include of my webview layout and this caused it to have a height of 0px. By setting this to fill_parent I resolved the issue. (Webview with white background and app with white background you cant really tell its not full height when it shows up as full height in the layout designer.)
Thanks for your help though!
clearView is deprecated.
Use:
webView.loadUrl("about:blank")
change wrap_content to fill_parent in your webview layout. it worked for me
use ip address instead of web url then override webclient using following ssl error handling method
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed(); // Ignore SSL certificate errors
}
I think What you need is WebViewClient. After setting webviewclient call webview.loadUrl(_url);
Somethin like this ....
private void loadUrlInWebView(String _URL){
WebView myWebView = (WebView) findViewById(R.id.webviewer);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.loadUrl(_URL);
}
private class MyWebViewClient extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
}
just use loadDataWithBaseURL
loadDataWithBaseURL(null,"html content" , "text/html" , "utf-8",null)
Try increasing the RAM for your emulated device. That solved the problem for me.
I found this out by looking at the logcat messages in Android Monitor (Android Studio). The messages below gave me the idea about what was wrong to increasing RAM :
W/art: Throwing OutOfMemoryError "Failed to allocate a 6635532 byte allocation with 1170528 free bytes and 1143KB until OOM"
W/JavaBrowserViewRendererHelper: Error allocating bitmap
E/chromium: [ERROR:java_browser_view_renderer_helper.cc(132)] Error unlocking java bitmap pixels.
My webview was displaying a white blank square on top of it, sometimes totally blank. This only occurred when it was loading certain data.
Changing it to a RelativeLayout, and setting layout_height to match_parent worked for me.
<RelativeLayout xmlns:android=...
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/ll_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="3dp"
android:gravity="center_vertical"
android:orientation="horizontal">
.....
</RelativeLayout>
<WebView
android:id="#+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/ll_title"
android:background="#color/colorYellow" />
<ProgressBar
android:id="#+id/pb_spinning"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="gone" />
</RelativeLayout>
You can use the following code, this will be helpful:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
In my case I was trying to load an html file. The solution came to be:
val webClient = object : WebViewClient() {
override fun onPageCommitVisible(view: WebView?, url: String?) {
super.onPageCommitVisible(view, myUrl)
binding.newsWebView.stopLoading()
}
}
binding.newsWebView.webViewClient = webClient
binding.newsWebView.loadUrl(myUrl)
After the html file has been loaded, it loaded about:blank and the screen was going blank.
I am loading a webpage into a webview from the database with the loadDataWithBaseURL() function and am trying to subsequently jump to an Anchor point. When I place a button just below the webview that uses the loadUrl()-function, it jumps to the Anchor point just fine. But if I place the loadUrl() after the loadDataWithBaseURL() it says page not found.
webview.loadDataWithBaseURL("app:myhtml", data, "text/html", "UTF-8", null);
webview.loadUrl("app:myhtml#tips");
I assumed it was because the load had not completed, and thus it couldn't find the Anchor, but I tried a loop to wait for it to getProgress() to equal 100, and have verified it's reaching 100 before that command executes, but it still won't find the page unless it's associated with the button click.
Any Ideas out there on how I can load a page from the database and then jump to an anchor point at the same time. I'm working with the Android 2.1 SDK.
Thanks to your help I got it to load directly to my anchor by adding this little bit of code. However I can't scroll around on the page, it starts to move and then snaps back. I suspect because it is reloading the page to that anchor each time. I'm fairly new to Android, and Java even, so this may be the wrong implementation of your suggestion, but I'd certainly appreciate a little more direction. Here is the code I used:
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(webview, "app://data");
webview.loadUrl("app://data#tips");
}
});
It seems this is making an infinite loop.
Try using valid URL as a base URL in loadDataWithBaseURL(), such as app://myhtml.
Also, to make sure the data was loaded implement WebViewCLient.onPageFinished().
It was getting in an infinite onPageFinished loop before. It had nothing to do with me scrolling, it was just continuously loading the same page. This code allows it to load without without getting stuck in a loop.
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if(url.contains("#") != true){
webview.loadUrl("app://data#tips");
}
}
});