I used below JSTest class and JSInterface to get javascript result
But "processJsReturnValue" function didn't be called.
What is my mistake?
public class JSTest {
private String jsReturnValue =null;
private WebView webView;
public Context mContext;
private static class JSInterface {
private JSTest myContext;
public JSInterface(JSTest context) {
this.myContext = context;
}
#JavascriptInterface
public void processReturnValue(String value) {
myContext.processJsReturnValue(value);
}
}
public JSTest(Context context) {
super();
mContext = context;
webView = new WebView(context);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new JSInterface(this), "androidInterface");
String jsString = "(function(){"+
"window.androidInterface.processReturnValue('abc');"+
"})();";
webView.loadUrl("javascript:"+jsString);
}
public void processJsReturnValue(String value) {
Log.e("got it", value);
jsReturnValue = value;
}
}
I solved it from #Jite's help
solution:
public JSTest(Context context) {
super();
mContext = context;
webView = new WebView(context);
// chromeClient = new CustomWebChromeClient();
//webView.setWebChromeClient(chromeClient);
webView.getSettings().setJavaScriptEnabled(true);
jsinterface = new JSInterface(this);
webView.addJavascriptInterface(jsinterface, "androidInterface");
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
Log.e("", "do loading");
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.e("", "do loading finished");
String jsString = "window.androidInterface.processReturnValue('abc');";
webView.loadUrl("javascript:"+jsString);
}
});
webView.loadData("<html></html>", "text/html", "utf8");
}
Related
Although I have tried many things, I cannot fix it. I apply the methods with Alt + Enter, I make the necessary arrangements on the added classes, but I still did not get a smooth result.
Error code;
Class 'Anonymous class derived from FirebaseRecyclerAdapter' must
either be declared abstract or implement abstract method
'onBindViewHolder (VH, int, T)' in 'FirebaseRecyclerAdapter'
Activity.Java
public class PlacesActivity extends AppCompatActivity {
private RecyclerView mBlogList;
private DatabaseReference mDatabase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_places);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Global");
mDatabase.keepSynced(true);
mBlogList = (RecyclerView) findViewById(R.id.placesRecyclerView);
mBlogList.setHasFixedSize(true);
mBlogList.setLayoutManager(new LinearLayoutManager(this));
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Blog, BlogViewHolder>firebaseRecyclerAdapter= new FirebaseRecyclerAdapter<Blog, BlogViewHolder>
(Blog.class, R.layout.blog_row,BlogViewHolder.class,mDatabase) {
protected void populateViewHolder(Blog model, BlogViewHolder viewHolder, int position){
viewHolder.setTitle(model.getTitle());
viewHolder.setDesc(model.getDesc());
viewHolder.setImage(getApplicationContext(), model.getImage());
}
};
firebaseRecyclerAdapter.startListening();;
mBlogList.setAdapter(firebaseRecyclerAdapter);
}
public static class BlogViewHolder extends RecyclerView.ViewHolder
{
View mView;
public BlogViewHolder(View itemView)
{
super(itemView);
mView=itemView;
}
public void setTitle(String title)
{
TextView post_title = (TextView)mView.findViewById(R.id.post_title);
post_title.setText(title);
}
public void setDesc(String desc){
TextView post_desc = (TextView)mView.findViewById(R.id.post_desc);
post_desc.setText(desc);
}
public void setImage(Context ctx, String image)
{
ImageView post_image = (ImageView)mView.findViewById(R.id.post_image);
Picasso.with(ctx).load(image).into(post_image);
}
}
}
Blog.java
public class Blog {
private String title;
private String desc;
private String image;
public Blog(String title, String desc, String image) {
this.title = title;
this.desc = desc;
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public Blog(){
}
}
populateRecyclerView
I have basic android webview.it is a signup page.i want to close this webview after signup and start new intent.im new to this please help.this is my code.
public class signupweb extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signupweb);
webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl("http://aaa.freeasphost.net/signup.aspx");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if (url.equals("http://aaa.freeasphost.net/MainPanel.aspx")) {
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(intent);
return false;
} else {
return true;
}
}
// }
I tried this but not working.it stuck in the signup page
You need to pass context to start new activity. Try this:
private class MyWebViewClient extends WebViewClient {
private Context context;
public MyWebViewClient(Context context) {
this.context = context;
}
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if (url.equals("http://aaa.freeasphost.net/MainPanel.aspx")) {
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
context.startActivity(intent);
return false;
} else {
return true;
}
}
I'm using android studio
I need to replace the body of an html document with other codes, this is an example that I tried to follow
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grafico);
wv = (WebView) findViewById(R.id.prova);
wv.setWebViewClient(new MyWebViewClient());
new BackgroundWorker().execute();
}
// load links in WebView instead of default browser
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
#RequiresApi(21)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return false;
}
}
private class BackgroundWorker extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... arg0) {
getDarewod();
return null;
}
public void getDarewod() {
try {
Document htmlDocument = Jsoup.connect(url).get();
Element element = htmlDocument.select("#gkHeaderMod > div.grafico-quotazione").first();
// replace body with selected element
htmlDocument.body().empty().append(element.toString());
final String html = htmlDocument.toString();
uiHandler.post(new Runnable() {
#Override
public void run() {
wv.loadData(html, "text/html", "UTF-8");
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
I want to replace the all body with the code in my element variable. However this line doesn't works. How can I do?
htmlDocument.body().empty().append(element.toString());
I want to show PopupMenu in webview on my app like GMail.
Here is my code:
webView.addJavascriptInterface(new WebAppInterface(getActivity()), "ScriptToAndroid");
<div class="moreover_icon" onclick="showPopup('file_attach_path', event)">
<img class="overflow_icon" src="android_moreover_icon"/>
</div>
<script type="text/javascript">
function showPopup(file_path, event) {
ScriptToAndroid.showPopupDialog(file_path, pos_x, pos_y);
}
</script>
#JavascriptInterface
public void showPopupDialog(String filePath, int x, int y) {
//show PopupMenu
}
Use a WebViewClient
mWebView = (WebView) findViewById(R.id.webView);
mWebView.setWebViewClient(new WebClient(this));
intercept shouldOverrideUrlLoading()
public class WebClient extends WebViewClient {
MainActivity mainActivity;
public WebClient(MainActivity activity) {
mainActivity = activity;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("xxx")) {
Log.i("show pop", "xx");
}
return false;
}
#Override
public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
return super.shouldOverrideKeyEvent(view, event);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
}
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
web = (WebView) findViewById(R.id.webview1);
web.setWebChromeClient(new MyWebChromeClient());
web.addJavascriptInterface(new DemoJavaScriptInterface(), "temp_1");
web.loadUrl("file:///android_asset/temp_1.html");
}
}
final class DemoJavaScriptInterface {
private Handler mHandler = new Handler();
WebView web;
DemoJavaScriptInterface() {
}
public void clickOnAndroid() {
mHandler.post(new Runnable() {
public void run() {
web.loadUrl("javascript:init();");
}
});
}
}
final class MyWebChromeClient extends WebChromeClient {
private static final String LOG_TAG = "WebViewDemo";
#Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Log.e(LOG_TAG, message);
result.confirm();
return true;
}
}
Try to write this line
web = (WebView) findViewById(R.id.webview1);
web.setWebChromeClient(new MyWebChromeClient());
web.getSettings().setJavaScriptEnabled(true);
web.addJavascriptInterface(new DemoJavaScriptInterface(), "temp_1");
web.loadUrl("file:///android_asset/temp_1.html");
[2]: Both Elements are coming from html file ,when going to click on "Click It" nothing happens instead of displaying xml data that i want