I followed an instruction to trigger the JavascriptInterface from a webview, but it isnt triggered in my case.
I have an class QuickTextViewer with following:
public class QuickTextViewer {
private WebView webView;
...
...
public QuickTextViewer(){
webView = dialog.findViewById(R.id.mywebview);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:MyApp.resize(document.body.getBoundingClientRect().height)");
super.onPageFinished(view, url);
}
}
#JavascriptInterface
public void resize(final float height) {
System.out.print(height);
}
I also added the following to proguard-rules.pro (actually public only for testing)
-keepclassmembers class fqcn.of.javascript.interface.for.webview {
public *;
}
In my case onPageFinished is triggered but resize() not !
Any suggestions/help ?
Found the problem now.
Just had to add:
webView.getSettings().setJavaScriptEnabled(true);
Related
I want to remove the header of the website in android 'WebView'. With the Code that I have, it works. But the problem is that the 'WebView' is removing the header after the page loaded completely. I want to remove it, while it is loading.
Thats the code snippet:
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url)
{
webView.loadUrl("javascript:(function() { " +
"var head = document.getElementsByTagName('header')[0];"
+ "head.parentNode.removeChild(head);" +
"})()");
webView.loadUrl("javascript:(function() { " +
"var head = document.getElementsByTagName('footer')[0];"
+ "head.parentNode.removeChild(head);" +
"})()");
}
});
ANDROID WebView-Remove header or any tag from Webpage in Android
Let's understand how's it possible, in my case I had loaded url in webview and look like below screenshot,
Now from the above view if we want to remove the header and menu
portion of the Webpage. We need to use JSOUP lib with the help of
which you can remove the unnecessary portion of webpage.
STEPS
Add jsoup lib in your app level build gradle
compile 'org.jsoup:jsoup:1.10.3'
Now In activity we will use Async task as we parse / remove some html tags that leads to network operations which cannot be done directly on UI Thread. Async Task allows you to perform background operations and publish results on the UI thread. It has three methods commonly used.
onPreExecute():-Is used to display progress bar
doInBackground():-This step is used to perform background computation that can take a long time. You cannot update any UI in this method.
onPostExecute():-This is invoked after completion of background task and it will update the UI.
I have defined WebView in xml like following way,
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webview"/>
And in MainActivity.java, I had wrote JSOUP code for remove the unnecessary portion of webpage
public class MainActivity extends AppCompatActivity {
WebView webview;
String url="http://pixelay.com/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview= (WebView) findViewById(R.id.webview);
new MyAsynTask().execute();
}
private class MyAsynTask extends AsyncTask<Void, Void, Document> {
#Override
protected Document doInBackground(Void... voids) {
Document document = null;
try {
document= Jsoup.connect(url).get();
document.getElementsByClass("main-navigation").remove();
document.getElementsByClass("custom-header-image").remove();
} catch (IOException e) {
e.printStackTrace();
}
return document;
}
#Override
protected void onPostExecute(Document document) {
super.onPostExecute(document);
webview.loadDataWithBaseURL(url,document.toString(),"text/html","utf-8","");
webview.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
webview.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, request);
}
});
}
}
}
Now, it's time to see the result,
Easiest way is to inject Javascript in onLoadResource() method. Put it inside try-catch block since WebView will not know about the element before it has been loaded:
webView.setWebChromeClient(new WebChromeClient() {
...
#Override
public void onLoadResource(WebView view, String url) {
try {
webView.loadUrl("javascript:(window.onload = function() { " +
"(elem1 = document.getElementById('id1')); elem.parentNode.removeChild(elem1); " +
"(elem2 = document.getElementById('id2')); elem2.parentNode.removeChild(elem2); " +
"})()");
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class YourwebView extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl());
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:var footer = document.getElementById(\"footer\"); footer.parentNode.removeChild(footer); var header = document.getElementById(\"header-full\"); header.parentNode.removeChild(header);");
}
}
And also make sure you have enabled javscript
YourwebView myWebClient = new YourwebView();
webview.setWebViewClient(myWebClient);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
Use onPageStarted method instead of onPageFinished.
From the Android Docs here
EDIT: Reading the comments on other answers, I see you want the header to show again. Simply implement both onPageStarted and onPageEnded, hiding the header in one and showing it again in the other.
This question already has answers here:
ProGuard obfuscating classes even after `keep class` flag. Affecting Android WebView behavior
(2 answers)
Closed 7 years ago.
After using pro guard java script interface method not working in release apk and same code working perfectly in development mode.
please help where i am wrong!
Code is below
public class Tab_Login extends Fragment
{
View rootView;
MyJavaScriptInterface myJavaScriptInterface;
static WebView myWebView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
rootView = inflater.inflate(R.layout.login, container, false);
myWebView.loadUrl("file:///android_asset/Login.html");
myJavaScriptInterface = new MyJavaScriptInterface(getActivity());
myWebView.addJavascriptInterface(myJavaScriptInterface, "Android");
}
public class MyJavaScriptInterface
{
Context mContext;
public MyJavaScriptInterface(Context c)
{
this.mContext = c;
}
#JavascriptInterface
public void loginBuy()
{
try
{
getActivity().runOnUiThread(new Runnable()
{
#Override
public void run()
{
// TODO Auto-generated method stub
Toast.makeText(mContext, "Method call", Toast.LENGTH_LONG).show();
}
});
}
catch (Exception e)
{
}
}
}
}
For proguard code below
// open this line
proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
// add these line
-keep public class com.xyz.Tab_Login.MyJavaScriptInterface
-keep public class * implements com.xyz.Tab_Login.MyJavaScriptInterface
-keepclassmembers class com.xyz.Tab_Login.MyJavaScriptInterface {
<fields>;
<methods>;
}
-keepclassmembers class * {
#android.webkit.JavascriptInterface <methods>;
}
-keepattributes JavascriptInterface.
Use below line and check
myWebView.getSettings().setJavaScriptEnabled(true);
I am trying to show a preloader before loading the complete page as webview but here is a small problem which I already made a preloader but it goes on and on and there is no end to it .
How can I tell the script to stop showing dialog when the page is loaded .
public class EzFragment extends Fragment {
public ProgressDialog pDialog;
public EzFragment(){}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new getFeed().execute();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_ez, container, false);
WebView myWebView = (WebView) rootView.findViewById(R.id.webView1);
String summary = "<html><head> <link rel=\"stylesheet\" type=\"text/css\"
href=\"http://www.example.com/demo/android_connect/assets/style/jquery.fullPage.css\" /></head></html>";
myWebView.loadData(summary, "text/html", null);
myWebView.loadUrl("http://www.example.com/demo/android_connect/get_ez_webmob.php");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
return rootView;
}
private class getFeed extends AsyncTask<Void, Void, Document> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setTitle("Connect to Server");
pDialog.setMessage("This process can take a few seconds to a few minutes, depending on your Internet Connection Speed.");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected Document doInBackground(Void... arg0) {
// TODO Auto-generated method stub
return null;
}
}
Is it possible to add webview to Document doInBackground in order to load the page in background ?!
How can I tell the script to stop showing dialog when the page is
loaded .
Use onPageFinished to dismiss ProgressDialog because onPageFinished method is called when page is fully loaded in WebView :
myWebView.setWebViewClient(new WebViewClient(){
//.....
#Override
public void onPageFinished(WebView view, String url) {
Log.d("mytag","Page Loading Finished!");
super.onPageFinished(myWebView, url);
// dismiss ProgressDialog here
}
});
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (pDialog.isShowing()) {
pDialog.dismiss();
}
}
Stop the dialog in postexecute..
I have a webView in an Activity. Inside, the webView I have a button that opens a new HTML page. When this button is pressed and the new html page opens I would like the screen orientation to change to horizontal.
var searchButton = $('<button/>',
{
text:'Search!',
"class":"buttons",
id: "searchButtonID",
click: function(){
var select = document.getElementById("subCategory");
var option = select.options[select.selectedIndex];
var subCategoryId = option.id;
window.location.href = "deals.html?subCategoryId=" + subCategoryId;
Android.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
});
I recently learnt about WebAppInterfaces that allows Javascript to interact with Android. I tried adding the line:
Android.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
However, I get an error message saying:
Uncaught ReferenceError: ActivityInfo is not defined
I imported import android.content.pm.ActivityInfo; in the relevant class.
Not sure if what I'm doing is even possible...
Any help would be greatly appreciated!
private void rotate(float degree) {
final RotateAnimation rotateAnim = new RotateAnimation(0.0f, degree,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
rotateAnim.setDuration(0);
rotateAnim.setFillAfter(true);
oWebView.startAnimation(rotateAnim);
}
btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
rotate(90);
}
});
try this
Do something like this
boolean rotationRequired = false;
private void openWebviewDialog(String days)
{
dialog = new Dialog(MainActivity.GLOBAL_CONTEXT);
dialog.setContentView(R.layout.simple_webview);
WebView wb = (WebView) dialog.findViewById(R.id.simple_webview);
WebSettings webSettings = wb.getSettings();
wb.setWebViewClient(new MyWebViewClient());
webSettings.setJavaScriptEnabled(true);
wb.addJavascriptInterface(this, "javaScriptInterface");
String url = "http://www.yourdomain.com" // your url here
String title = "Hello";
wb.loadUrl(url);
dialog.setCancelable(true);
dialog.setTitle(title);
dialog.show();
}
class MyWebViewClient extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(url.contains("openExternal"))
{
return super.shouldOverrideUrlLoading(view, url); // Leave webview and use browser
}
else
{
view.loadUrl(url); // Stay within this webview and load url
return true;
}
}
#Override
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view, url);
if(rotationRequired)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
super.onPageStarted(view, url, favicon);
}
}
#JavascriptInterface
public void passResult(String status)
{
rotationRequired = status.equalsIgnoreCase("true") ? true : false;
}
and don't forget to add this to your manifest:
android:configChanges = "orientation"
control the rotation as
<p>rotate...</p><script type='text/javascript' language='javascript'>javaScriptInterface.passResult('true');</script>
Please define and import packages
WebView myWebView;
WebAppInterface mWebAppInterface;
add this code to onCreate() of your activity
mWebAppInterface = new WebAppInterface(this);
myWebView = (WebView) findViewById(R.id.webview);
myWebView.addJavascriptInterface(mWebAppInterface, "Android1");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//load your URL by myWebView.loadUrl("www.example.com");
Add this public function also
#JavascriptInterface
public void setorientation(){
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
Now you can call this public function for setting orientation from javascript button click function
Android1.setorientation();
visit also
http://developer.android.com/guide/webapps/webview.html
using javascript in android webview
I want to load a url by webView.
The url is http://wapp.baidu.com/f?kw=%BB%F0%BC%FD ,this page can work correctly on system default browser, but in my webView, some JavaScript not working.
JavaScript is enabled, and I dont know what's wrong with it.
I would appreciate any help.
private void initUI() {
webView = (WebView) findViewById(R.id.web_view);
webView.getSettings().setJavaScriptEnabled(true);
final NavigationBar navigationBar = (NavigationBar) findViewById(R.id.navigationbar);
navigationBar.refreshUI();
navigationBar.rightButton.setImageResource(R.drawable.refresh);
navigationBar.rightButton.setVisibility(View.VISIBLE);
navigationBar.rightButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
refresh();
}
});
navigationBar.leftButton.setImageResource(R.drawable.back);
navigationBar.leftButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
webView.goBack();
}
});
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
navigationBar.leftButton.setVisibility(webView.canGoBack() ? View.VISIBLE
: View.INVISIBLE);
super.onPageFinished(view, url);
}
});
refresh();
}
update: the problem is solved
just add following code:
webView.getSettings().setDomStorageEnabled(true);
If you're using proguard, it stops javascript running. For instance, in my proguard-project.txt file I have the following:
if your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
I think there is some issue with javascript file caching/refreshing. I changed the filename of js & it starts working fine.