This code is not working and my paystack payment gateway is not showing, though it displays the spinner but will not show the form for card input.
Please I need a complete code that will help me get my job done:
webView.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onCreateWindow(WebView view, boolean isDialog,
boolean isUserGesture, Message resultMsg) {
WebView newWebView = new WebView(WebpageActivity.this);
newWebView.getSettings().setJavaScriptEnabled(true);
newWebView.getSettings().setSupportZoom(true);
newWebView.getSettings().setBuiltInZoomControls(true);
newWebView.getSettings().setPluginState(PluginState.ON);
newWebView.getSettings().setSupportMultipleWindows(true);
view.addView(newWebView);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(newWebView);
resultMsg.sendToTarget();
newWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
return true;
}
}
});
Well, this code should work when pasted on your MainActivity file of your android webview project.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.net.Uri;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.PluginState;
public class MainActivity extends Activity {
private WebView webView = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webView.setInitialScale(1);
webSettings.setJavaScriptEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webSettings.setAllowFileAccess(true);
webSettings.setAllowContentAccess(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setPluginState(PluginState.ON);
webSettings.setSupportMultipleWindows(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
WebViewClientImpl webViewClient = new WebViewClientImpl(this);
webView.setWebViewClient(webViewClient);
webView.loadUrl("https://www.your_url.com/");
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && this.webView.canGoBack()) {
this.webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
public class WebViewClientImpl extends WebViewClient {
private Activity activity = null;
public WebViewClientImpl(Activity activity) {
this.activity = activity;
}
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if(url.indexOf("your_url") > -1 ) return false;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
activity.startActivity(intent);
return true;
}
}
}
Now, after pasting the above code you need to use the PHP paystack integration this time and not the javascript paystack integration initialization and callback functions.
At this point you only need to worry about your sessions.
Tested And Confirmed.
Related
import android.content.Context;
import android.graphics.Color;
import android.icu.text.SimpleDateFormat;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.github.sundeepk.compactcalendarview.CompactCalendarView;
import com.github.sundeepk.compactcalendarview.domain.Event;
import java.util.Date;
import java.util.Locale;
public class Calendarpage extends AppCompatActivity {
//modify here
ActionBar actionBar = null;
CompactCalendarView compactCalendar;
private SimpleDateFormat dateFormatMonth = new SimpleDateFormat ("MMM- yyyy", Locale.getDefault());
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar);
// modify here
ActionBar myActionBar = getSupportActionBar();
if (myActionBar != null) {
myActionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle(null);
}
compactCalendar = (CompactCalendarView) findViewById(R.id.compactcalendar_view);
compactCalendar.setUseThreeLetterAbbreviation(true);
Event ev1 = new Event(Color.RED, 1514160000L, "Chirstmas Day");
compactCalendar.addEvent(ev1);
compactCalendar.setListener(new CompactCalendarView.CompactCalendarViewListener() {
#Override
public void onDayClick(Date dateClicked) {
Context context = getApplicationContext();
if (dateClicked.toString().compareTo("Mon Dec 25 00:00:00 AST 2017") == 0) {
Toast.makeText(context, "Christmas Day", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "No Events Planned That Day", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onMonthScroll(Date firstDayOfNewMonth) {
getSupportActionBar.setTitle(String.valueOf(dateFormatMonth.format(firstDayOfNewMonth)));
}
});
}
}
now added the changes you made but the getSupportActionBar. is in red? its just asking me to create local variables when right clicking. very stressed with this as I can't move on until this page works.. as I still need to be able to add events to dates
Try to make this change
In your OnCreate :
ActionBar myActionBar = getSupportActionBar();
if (myActionBar != null) {
myActionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle(null);
}
In your setListener
#Override
public void onMonthScroll(Date firstDayOfNewMonth) {
getSupportActionBar().setTitle(String.valueOf(
dateFormatMonth.format(firstDayOfNewMonth)));
}
Good luck
EDIT try this
compactCalendar.setListener(new CompactCalendarView.CompactCalendarViewListener() {
#Override
public void onMonthScroll(Date firstDayOfNewMonth) {
setNewTitleBar(dateFormatMonth.format(firstDayOfNewMonth));
}
});
}
private void setNewTitleBar(String newTitle) {
getSupportActionBar().setTitle(newTitle);
}
}
I have a JavaFX WebView and want to call the method "hello" of the class "JavaBridge" from "test.html" displayed in the webview.
Why doesn't this work? I making sure that the "bridge" object only be added to the window.object when the page has been fully rendered, so that is probably not the problem. I can't see any problem with the HTML either.
Here is the HTML code ("test.html"):
<html>
<head>
</head>
<body>
call java
</body>
</html>
And here is the Java Code:
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import netscape.javascript.JSObject;
public class HelloWorld extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
java.net.URI uri = java.nio.file.Paths.get("test.html").toAbsolutePath().toUri();
WebView root = new javafx.scene.web.WebView();
root.getEngine().load(uri.toString());
root.getEngine().
getLoadWorker().
stateProperty().
addListener(new ChangeListener < State > () {
#Override public void changed(ObservableValue ov, State oldState, State newState) {
if (newState == Worker.State.SUCCEEDED) {
System.out.println("READY");
JSObject jsobj = (JSObject) root.getEngine().executeScript("window");
jsobj.setMember("bridge", new JavaBridge());
}
}
});
primaryStage.setScene(new javafx.scene.Scene(root, 800, 600));
primaryStage.show();
}
}
class JavaBridge {
public void hello() {
System.out.println("hello");
}
}
When using this bridge feature on Java 10.0.2, I noticed that it was not working consistently. Javascript upcalls wasn't working all the times.
After researching, I found out this OpenJDK bug related to Java Garbage Collector, which seems to happen on regular JDK as well:
https://bugs.openjdk.java.net/browse/JDK-8170085
Indeed, according to https://docs.oracle.com/javase/9/docs/api/javafx/scene/web/WebEngine.html, it's recommended to store the bridge into a variable to avoid Java GC to collect the object.
After adding a private variable to the class, the JS to Java calls started to work all the time in my Application.
Your inner class should be inside the main class. And it should be public. Like this:
import java.net.URL;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import netscape.javascript.JSObject;
public class HelloWorld extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
final URL url = getClass().getResource("test.html");
WebView root = new javafx.scene.web.WebView();
root.getEngine().load(url.toExternalForm());
root.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
#Override
public void changed(ObservableValue ov, State oldState, State newState) {
if (newState == Worker.State.SUCCEEDED) {
System.out.println("READY");
JSObject jsobj = (JSObject) root.getEngine().executeScript("window");
jsobj.setMember("bridge", new JavaBridge());
}
}
});
primaryStage.setScene(new javafx.scene.Scene(root, 800, 600));
primaryStage.show();
}
public class JavaBridge {
public void hello() {
System.out.println("hello");
}
}
}
I had the same problem and the only way to fix it was storing the Bridge on a static variable.
this is an example to use the javafx FileChooser.
public class Controller implements Initializable {
#FXML private WebView webview;
#FXML private JFXButton btn_insertimg;
#FXML private AnchorPane anchorpane;
private WebEngine webEngine;
public static Bridge bridge; //it's important to be static
#Override
public void initialize(URL location, ResourceBundle resources) {
webEngine = webview.getEngine();
webEngine.getLoadWorker().stateProperty().addListener(
(ov, oldState, newState) -> {
if (newState == Worker.State.SUCCEEDED) {
//todo when the document is fully loaded
FileChooser fileChooser=new FileChooser();
bridge=new Bridge(webview,fileChooser);
JSObject window = (JSObject) webEngine.executeScript("window");
window.setMember("myFileChooser", bridge);
System.out.println("member "+window.getMember("myFileChooser").toString());;
}//end of SUCCEEDED state
});
webEngine.load(getClass().getResource("/patient/texteditor/summernote.html").toExternalForm());
}
public class Bridge{
FileChooser fileChooser;
WebView webView;
Bridge(WebView webView,FileChooser fileChooser){
this.webView=webView;
this.fileChooser=fileChooser;
}
public void display(){
fileChooser.showOpenDialog(webView.getScene().getWindow());
}
}
}
I had Successful login to a website using jsoup and I obtained cookies from Jsoup cookies function in Map but I am struck at something.After successful login I Want to open a webpage of the website in the webview but the webpage redirects to the login page because cookies are not successfully passed to the webview and I don't know why. The page wants cookies of successful login to load that page
Here is the login page url.
http://111.68.99.8/StudentProfile/
and page I want to load after Successful Login
http://111.68.99.8/StudentProfile/PersonalInfo.aspx
Here is the MainActivity.java
package com.example.ebad.bu;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends ActionBarActivity {
String url = "http://111.68.99.8/StudentProfile/";
Map<String, String> logingcookies;
HashMap<String,String> hashMap;
ProgressDialog progressDialog,progressDialoge;
Button login, personal;
TextView Enrollement, password, E;
String enrol = "";
String pass = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (Button) findViewById(R.id.login_button);
personal = (Button) findViewById(R.id.pers);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Enrollement = (TextView) findViewById(R.id.Enrollment);
enrol = Enrollement.getText().toString();
password = (TextView) findViewById(R.id.password);
pass = password.getText().toString();
new Title().execute();
}
});
}
private class Title extends AsyncTask<Void, Void, Void> {
String title;
String father;
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Title");
progressDialog.setMessage("Loading");
progressDialog.setIndeterminate(false);
progressDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
try {
Connection.Response loginForm = Jsoup.connect(url).method(Connection.Method.GET).timeout(1000).execute();
Document doc = loginForm.parse();
String viewstate = doc.select("input[name=__VIEWSTATE]").attr("value");
String stategenerator = doc.select("input[name=__VIEWSTATEGENERATOR]").attr("value");
String Eventvalidation = doc.select("input[name=__EVENTVALIDATION]").attr("value");
doc = Jsoup.connect(url)
.data("__LASTFOCUS", "")
.data("__EVENTTARGET", "")
.data("__EVENTARGUMENT", "")
.data("__VIEWSTATE", viewstate)
.data("__VIEWSTATEGENERATOR", stategenerator)
.data("__EVENTVALIDATION", Eventvalidation)
.data("ctl00$Body$ENROLLMENTTextBox$tb", enrol)
.data("ctl00$Body$ENROLLMENTTextBox$cv_vce_ClientState", "")
.data("ctl00$Body$PasswordTextBox$tb", pass)
.data("ctl00$Body$PasswordTextBox$cv_vce_ClientState", "")
.data("ctl00$Body$LoginButton", "Login")
.cookies(loginForm.cookies())
.post();
logingcookies = loginForm.cookies();
title = doc.select("div[id=ctl00_Accounts]").html();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
/*Intent showadditionmenu = new Intent(MainActivity.this,Per.class);
//showadditionmenu.putStringArrayListExtra("logingcookies", (ArrayList<String>) logingcookies);
showadditionmenu.putExtra("logingcookies", String.valueOf(logingcookies));
MainActivity.this.startActivity(showadditionmenu);*/
/* Intent i = new Intent(MainActivity.this,Per.class);
i.putExtra("hashMap",hashMap );
startActivity(i);
*/
hashMap = new HashMap<String,String>(logingcookies);
Intent i = new Intent(MainActivity.this,wee.class);
i.putExtra("hashMap",hashMap );
startActivity(i);
/* E = (TextView) findViewById(R.id.message);
E.setText(title);
progressDialog.dismiss();*/
}
}
}
I have passed the saved cookie to the activity that load the webpage in webivew.. Here is the other activity;
Wee.java
package com.example.ebad.bu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Ebad on 11/8/2015.
*/
public class wee extends Activity {
String url= "http://111.68.99.8/StudentProfile/PersonalInfo.aspx";
HashMap<String ,String> hashMap;
Map<String,String> map;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
Intent intent = getIntent();
hashMap = (HashMap<String,String>) intent.getSerializableExtra("hashMap");
WebView webView = (WebView) findViewById(R.id.webviewe);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(url,hashMap);
}
}
Is there any way that I can view that webpage without logging in again to the webview?
Haven't tested it, but using CookieManager ahould work.
Usage should be something like this:
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie(); //optional
cookieManager.setAcceptCookie(true);
Then loop over your cookies using the setCookie method.
Note: If useing lollipop and up you should also use setAcceptThirdPartyCookies.
Good luck.
Some type of Urls are not loading in my app in Webview but it can be loaded in device browser. Following is the example Url which is not working in my code-
"http://apps.takeyourapp.com/testApp/staging/index.html"
package com.example.webviewdemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.LinearLayout.LayoutParams;
public class MainActivity extends Activity {
WebView webViewPlaceholder;
String URL = "http://apps.takeyourapp.com/testApp/staging/index.html";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webViewPlaceholder = (WebView) findViewById(R.id.webholder);
webViewPlaceholder.getSettings().setJavaScriptEnabled(true);
webViewPlaceholder
.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webViewPlaceholder.setScrollbarFadingEnabled(true);
webViewPlaceholder.getSettings().setLoadsImagesAutomatically(true);
webViewPlaceholder.getSettings().setUseWideViewPort(true);
webViewPlaceholder.loadUrl(URL);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Create WebViewClient and load url like
public class myWebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
And setWebViewClient like:
webView.setWebViewClient(new myWebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://apps.takeyourapp.com/testApp/staging/index.html");
And also add INTERNET permission into manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
Try this solution.
public class Main extends Activity {
private WebView mWebview ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("http://apps.takeyourapp.com/testApp/staging/index.html");
setContentView(mWebview );
}
}
I am calling javascript function from an activity but I am getting Uncaught ReferenceError: myFunction is not defined at null:1 error. Here is my files
MainActivity.java
package com.example.androidexample;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webview = (WebView)this.findViewById(R.id.webView1);
WebSettings webSettings = webview.getSettings();
// Enable Javascript for interaction
webSettings.setJavaScriptEnabled(true);
// Make the zoom controls visible
webSettings.setBuiltInZoomControls(true);
// Allow for touching selecting/deselecting data series
webview.requestFocusFromTouch();
// Set the client
webview.setWebViewClient(new WebViewClient());
webview.setWebChromeClient(new WebChromeClient());
//webview.loadUrl("file:///android_asset/test.html");
/*String str = "<books>" +
"<book title=\"A Tale of Two Cities\"/>" +
"<book title=\"1984\"/>" +
"</books>";*/
webview.loadUrl("file:///android_asset/test.html");
webview.loadUrl("javascript:myFunction()");
//webview.loadUrl("javascript:myFunction("+str+")");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
test.html
<html>
<body>
<script type="text/javascript" src="marknote.js"></script>
<script type="text/javascript">
function myFunction()
{
var str = '<books><book></book></books>';
var parser = new marknote.Parser();
var doc = parser.parse(str);
var bookEls = doc.getRootElement().getChildElements();
for (var i=0; i<bookEls.length; i++) {
var bookEl = bookEls[i];
alert("Element name is " + bookEl.getName());
}
}
</script>
</body>
</html>
I have seen this error occurred when I am using webview.loadUrl("javascript:myFunction()");. I want to pass a xml string from java and parse it into javascript. Please help me to find a solution.
You should execute the javascript when the page is loaded
webview.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url){
webview.loadUrl("javascript:myFunction()");
}
});
The code will be executed and will find your javascript function. The way you are doing now does not wait.
This one worked just need to change parameter set up sequence
package com.example.androidexample;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView webview = (WebView)this.findViewById(R.id.webView1);
WebSettings webSettings = webview.getSettings();
// Enable Javascript for interaction
webSettings.setJavaScriptEnabled(true);
// Make the zoom controls visible
webSettings.setBuiltInZoomControls(true);
// Allow for touching selecting/deselecting data series
webview.requestFocusFromTouch();
// Set the client
// webview.setWebViewClient(new WebViewClient());
// webview.setWebChromeClient(new WebChromeClient());
final String str = "<books>" +
"<book title=\"A Tale of Two Cities\"/>" +
"<book title=\"1984\"/>" +
"</books>";
// webview.loadUrl("file:///android_asset/test.html");
webview.setWebChromeClient(new WebChromeClient());
webview.setWebViewClient(new WebViewClient()
{
public void onPageFinished(WebView view, String url)
{
webview.loadUrl("javascript:myFunction('"+str+"')");
}
}
);
webview.loadUrl("file:///android_asset/test.html");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}