Below is my code of android which and i want same thing in
ios(objective-c)
private class MyWebChromeClient extends WebChromeClient {
//display alert message in Web View
#Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
new AlertDialog.Builder(view.getContext())
.setMessage(message).setCancelable(true).show();
result.confirm();
return true;
}
}
Related
I Have Made A Webview App For My Website.
I Have Added Fuction For Webview Confirmation But I Want To Customize That Webview Confirmation Dialogbox.Is There Any Way To Do It ?
Here Is My Code For Webview Confimation Dialogbox :
webview = findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
webview.loadUrl(websiteURL);
webview.setWebViewClient(new WebViewClientDemo());
webview.setWebChromeClient(new WebChromeClient());
}
final Context myApp = this;
final class MyWebChromeClient extends WebChromeClient {
#Override
public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
new AlertDialog.Builder(myApp)
.setTitle("LinkShortify")
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
result.confirm();
}
})
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
result.cancel();
}
})
.create()
.show();
return true;
}
}
I Am Getting Message Like This :
This Is The Confirmation Dialogbox
Please Help Me With This To Customized Buttons, Message And Heading Of This Dialogbox.
Need Guidance To Make Code Better.
Need Help To Customize The Dialogbox.
When file is uploaded, a Button is enabled:
// myUploadComponent extends Upload
myUploadComponent.addSucceededListener(event -> enabledMyButtonMEthod ()); // working well
I don't find out how to disable that Button when I remove the file (click on the cross next to it).
There should be something like 'addRemoveListener' ... ?
How can I detect this event ?
You can listen to the "file-remove" event in the upload component. Here is an example.
#Route("")
public class MainView extends VerticalLayout {
public MainView() {
MyUpload upload = new MyUpload();
upload.addFileRemoveListener(e -> Notification.show("Button disabled"));
add(upload);
}
class MyUpload extends Upload {
Registration addFileRemoveListener(ComponentEventListener<FileRemoveEvent> listener) {
return super.addListener(FileRemoveEvent.class, listener);
}
}
#DomEvent("file-remove")
public static class FileRemoveEvent extends ComponentEvent<Upload> {
public FileRemoveEvent(Upload source, boolean fromClient) {
super(source, fromClient);
}
}
}
I have added and event listener like so
upload
.getElement()
.addEventListener(
"file-remove",
event -> {
JsonObject eventData = event.getEventData();
String fileName = eventData.getString("event.detail.file.name");
// ...
}).addEventData("event.detail.file.name");
Found the solution here: https://github.com/vaadin/vaadin-upload/issues/347#issuecomment-516292999
I would try public Registration addChangeListener(Upload.ChangeListener listener) which should be triggered on the filename change event
I have extended Tulio's solution to get the removed file name as well in the FileRemoveEvent. Comes in very handy!
private class MyUpload extends Upload {
public MyUpload(MultiFileMemoryBuffer buffer) {super(buffer);}
Registration addFileRemoveListener(ComponentEventListener<FileRemoveEvent> listener) {
return super.addListener(FileRemoveEvent.class, listener);
}
}
#DomEvent("file-remove")
public static class FileRemoveEvent extends ComponentEvent<Upload> {
private String fileName;
public FileRemoveEvent(Upload source, boolean fromClient, #EventData("event.detail.file.name") JreJsonString fileNameJson) {
super(source, fromClient);
fileName = fileNameJson.getString();
}
public String getFileName() {
return fileName;
}
}
If I have created a deep link using the branch.io that opens a specific screen in my app. If this link is also available in my app and a user clicks on it, will it open my screen? or it will do nothing as the link I am trying to open from the app is pointing to the same app?
When you click on a Branch link within a webView in your App, you will have to handle the routing to the specific Activity, after reading the Branch link parameters.
Here is a sample Activity which contains a webView and and shows a couple of Branch links. When you click on a link in the webView it reopens the webview and displays the link parameters in a Toast message if a Branch link is clicked
public class MainActivity extends AppCompatActivity {
private WebView webView_;
private Button button_;
private String TAG = "WebViewController";
private Context context_;
private static final String URL_TO_LOAD = "https://evangelosg.github.io/index.html";
private static final String BRANCH_LINK_TO_LOAD = "https://ere6.app.link/b6sS0gsCfG";
#Override
protected void onNewIntent(Intent intent) {
Log.d("WebView", "onNewIntent");
setIntent(intent);
}
#Override
protected void onResume() {
super.onResume();
Branch branch = Branch.getInstance();
branch.initSession(new Branch.BranchReferralInitListener() {
#Override
public void onInitFinished(JSONObject referringParams, BranchError error) {
if (error == null) {
Log.d(TAG, referringParams.toString());
Toast.makeText(context_, referringParams.toString(), Toast.LENGTH_LONG).show();
if (referringParams.has(BundleExtraKeys.CLICKED_BRANCH_LINK)) {
try {
boolean clickedBranchLink = referringParams.getBoolean(BundleExtraKeys.CLICKED_BRANCH_LINK);
if (clickedBranchLink) {
//do stuff!
}
} catch (JSONException e) {
Log.d("BranchTrends", e.getMessage());
}
}
} else {
Log.i("MyApp", error.getMessage());
}
}
}, this.getIntent().getData(), this);
}
#Override
protected void onStart() {
super.onStart();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context_ = this;
setContentView(R.layout.activity_main);
webView_ = (WebView) findViewById(R.id.webView);
webView_.setWebViewClient(new BranchWebViewController("app.link", MainActivity.class));
webView_.loadUrl(URL_TO_LOAD);
button_ = (Button) findViewById(R.id.button);
button_.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.intent.putExtra("branch", BRANCH_LINK_TO_LOAD);
customTabsIntent.intent.putExtra("branch_force_new_session", true);
finish();
customTabsIntent.launchUrl(MainActivity.this, Uri.parse(BRANCH_LINK_TO_LOAD));
}
});
}
public class BranchWebViewController extends WebViewClient {
private String myDomain_;
private Class activityToLaunch_;
BranchWebViewController(#NonNull String myDomain, Class activityToLaunch) {
myDomain_ = myDomain;
activityToLaunch_ = activityToLaunch;
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
if (url.contains(myDomain_)) {
Intent i = new Intent(view.getContext(), activityToLaunch_);
i.putExtra("branch", url);
i.putExtra("branch_force_new_session", true);
finish();
startActivity(i);
} else {
view.loadUrl(url);
}
return true;
}
}
}
Once you read the link parameters you can route to the appropriate Activity based on the link parameters.
I experienced the following problem with PopupViews: My application has an error handler set in the UI and this error handler shows some error notification by invoking Notification.show(...) when it receives error event. In a popup view I have a button, which performs some action. When one clicks the button, the popup view is closed (by invoking setPopupVisible(false)) and the action is executed. However, if the action fails to run and throws an exception, I expect the exception to be handled by the UI and the error message is to be shown on the screen. Unfortunately, the handler receives the error event and invokes Notification.show, but no message is shown.
Has someone faced the similar issue?
You can use other notification system and run async JavaScript code.
Also you can wrap any other notification system in 10 minutes (implement Vaadin AbstractJavaScriptComponent), for example noty2 (for use should be included jQuery lib): http://needim.github.io/noty/
and show notif when you action executed like this:
new Notty("WARNING<br>description... (static)", Notty.Type.WARNING).show(UI.getCurrent());
new Notty("WARNING<br>description... (close in 3000ms)", Notty.Type.WARNING, 3000).show(UI.getCurrent());
new Notty("WARNING<br>description... (modal)", Notty.Type.WARNING, 0, true).show(UI.getCurrent());
new Notty("WARNING<br>description... (modal force, close in 3000ms)", Notty.Type.WARNING, 3000, true, true).show(UI.getCurrent());
new Notty("asd", Notty.Type.ERROR, 5000).show(UI.getCurrent());
package user_interface.util.ext;
import com.vaadin.annotations.JavaScript;
import com.vaadin.ui.AbstractJavaScriptComponent;
import com.vaadin.ui.AbstractOrderedLayout;
import com.vaadin.ui.UI;
import java.io.Serializable;
import java.util.Iterator;
public class Notty implements Serializable {
private String message;
private String type;
private int closeIn = 0;
private boolean force = false;
private boolean modal = false;
public Notty(String message, Notty.Type type) {
this.message = message;
this.type = type.value;
}
public Notty(String message, Notty.Type type, int closeIn) {
this.message = message;
this.type = type.value;
this.closeIn = closeIn;
}
public Notty(String message, Notty.Type type, int closeIn, boolean modal) {
this.message = message;
this.type = type.value;
this.closeIn = closeIn;
this.modal = modal;
}
public Notty(String message, Notty.Type type, int closeIn, boolean modal, boolean force) {
this.message = message;
this.type = type.value;
this.closeIn = closeIn;
this.modal = modal;
this.force = force;
}
public void show(UI ui) {
AbstractOrderedLayout aol = (AbstractOrderedLayout) ui.getContent();
NottyMessage nm = null;
boolean wasAdded = false;
Iterator itr = aol.iterator();
while(itr.hasNext()) {
Object o = itr.next();
if (o instanceof NottyMessage) {
nm = (NottyMessage) o;
wasAdded = true;
}
}
if (!wasAdded) {
nm = new NottyMessage();
aol.addComponent(nm);
}
nm.show(message, type, closeIn, force, modal);
}
#JavaScript({"NottyMessage.js"})
private class NottyMessage extends AbstractJavaScriptComponent {
public NottyMessage() {
setImmediate(true);
}
#Override
protected NottyMessageState getState() {
return (NottyMessageState) super.getState();
}
private void show(String mess, String type, int closeIn, boolean force, boolean modal) {
callFunction("show", mess, type, closeIn, force, modal);
}
}
public static enum Type {
ALERT("alert"),
INFORMATION("information"),
ERROR("error"),
WARNING("warning"),
NOTIFICATION("notification"),
SUCCESS("success");
private String value;
private Type(String val) {
this.value = val;
}
}
}
NottyMessageState.java
package user_interface.util.ext;
import com.vaadin.shared.ui.JavaScriptComponentState;
public class NottyMessageState extends JavaScriptComponentState {
public String xhtml;
public String type;
}
NottyMessage.js
user_interface_util_ext_Notty_NottyMessage = function() {
var e = this.getElement();
this.onStateChange = function() {
// change state callb
}
this.show = function(message, messageType, closeInMs, isForce, isModal) {
var n = noty({
text: message,
type: messageType,
dismissQueue: true,
timeout: closeInMs,
force: isForce,
modal: isModal,
maxVisible: 7,
layout: "bottomLeft",
theme: "defaultTheme"
});
}
}
it should look like this:
Maybe the handler is in a different thread and not in the UI thread. I got the same weird behavoir trying to enable a disabled button which did not worked until i used
Button button = new Button("foo")
// ...
getUI().access(new Runnable(){
public void run() {
button.setEnabled(true)
}
})
Here is My Code.
I just want to get the page content from the Webview after load the URL .
On Android, I have a WebView that is displaying a page.
How do I get the page source without requesting the page again?
It seems WebView should have some kind of WebResourceResponse () method in Android 3.0 but don't know how to use it.
public class WebActivity extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("https://xyz.com");
mWebView.setWebViewClient(new HelloWebViewClient());
}
private class HelloWebViewClient extends WebViewClient {
public WebResourceResponse shouldInterceptRequest (WebView view, String url)
{
return null;
}
#Override
public void onPageFinished(WebView view, String url)
{
/* This call inject JavaScript into the page which just finished loading. */
mWebView.loadUrl("javascript:window.HTMLOUT.processHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}}
I know this is old, but for future visitors, the following should work.
First, you need to define an interface such as:
final class LoadingInterface {
#JavascriptInterface
public void outputHTML(final String html){
//do what ever you want with html
}
}
Then on your WebView call
webView.addJavascriptInterface(new LoadingInterface(), "INTERFACE");
In onPageFinished put:
String js = "javascript:"
+"var html = document.getElementsByTagName('html')[0].innerHTML;"
+"window.INTERFACE.outputSchedule(html);";
webView.loadUrl(js);
If you only need part of a page, you can replace getElementsByTagName('html')[0] with something like getElementById('partOfPage')