I need to open a dialog in a website which opens on clicking a HTML button of the website. I'm trying to click on a HTML button based on the id from the fragment, how can I perform the action, I'm using
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// trying to click the button with id btn-to-be-clicked
webView.loadUrl("javascript:(function(){document.getElementById('btn-to-be-clicked').click();})()");
}
}
thanks
I tried
webView.loadUrl("javascript:(function(){document.getElementById('btn-to-be-clicked').click();})()");
so apparently you must specify every line in a new string
extracted the js to a variable with multi line, it has to be a multi line concatenated string
val jsFunction = "javascript:(function() { " +
"document.getElementById('btn-to-be-clicked').click();" +
"})()"
Related
I developed a simple webview app. I added a button which should act as the back button, so users can navigate back one page.
The button is initially hidden and should only show if a back history exists.
How can I realise this?
The code is simple:
backButton = findViewById(R.id.backButton);
backButton.setEnabled(blizzView.canGoBack());
but where do I have to call this? How is the "some site loaded" event called?
Update:
I tried to apply #Murats answer, but nothing happens:
private WebView blizzView; // my webview
private Button backButton;
public void onPageFinished(WebView view, String url)
{
backButton = findViewById(R.id.backButton);
backButton.setEnabled(blizzView.canGoBack());
if (blizzView.canGoBack()) {
backButton.setVisibility(View.VISIBLE);
} else {
backButton.setVisibility(View.INVISIBLE);
}
}
You can use WebView#canGoBack to find out if there is a backstack. You can check for that after the page is loaded e.g. in WebViewClient#onPageFinished
It works like this:
blizzView.setWebViewClient(new WebViewClient() { //
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
backButton = findViewById(R.id.backButton);
backButton.setEnabled(blizzView.canGoBack());
if (blizzView.canGoBack()) {
backButton.setVisibility(View.VISIBLE);
} else {
backButton.setVisibility(View.INVISIBLE);
}
}
});
I am building an android application where a URL is loaded on a webView. Now in that URL, there are some Hyperlink's which is loaded when a user clicks on them. While click I get that URL string and a load that URL in webView.
Now I need when a user clicks on that Hyperlink I need to get that text of that Hyperlink. I had to google a lot yet did not get any good answers.
This function will show all resources text(hyperlink, image, css file, js file)
webView.setWebViewClient(new WebViewClient(){
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
Log.d(TAG, "onLoadResource: " + url);
}
});
So you have to check before using it
if (!url.endsWith(".css")
&& !url.endsWith(".js")
&& !url.endsWith(".otherformat") ) {
// Show your url
}
my name is Dan and I am trying to invoke a click on a HTML element through code in my Android application. If I try to click it through pure Javascript the result won't be the same as it is when the user clicks it.
So, how to press elements from a Webview component through Java code? Thanks in advance.
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String link) {
return super.shouldOverrideUrlLoading(view, url);
}
});
I'm creating a sample webpage with button..this webpage am calling in Android using webview.
now when I click the button on webpage(that is html button). I should be able to execute some codes in Android..
How to proceed?
public class web extends Activity {
WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webdisplay);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://localhost/test.html");
valid = new Button(ctx);
valid.setOnClickListener(this);
refuse = new Button(ctx);
refuse.setOnClickListener(this);
}
}
We can detect following HTML elements as per Android API Document.
int ANCHOR_TYPE HitTestResult for hitting a HTML::a tag
int EDIT_TEXT_TYPE HitTestResult for hitting an edit text area
int EMAIL_TYPE HitTestResult for hitting an email address
int GEO_TYPE HitTestResult for hitting a map address
int IMAGE_ANCHOR_TYPE HitTestResult for hitting a HTML::a tag which contains HTML::img
int IMAGE_TYPE HitTestResult for hitting an HTML::img tag
int PHONE_TYPE HitTestResult for hitting a phone number
int SRC_ANCHOR_TYPE HitTestResult for hitting a HTML::a tag with src=http
int SRC_IMAGE_ANCHOR_TYPE HitTestResult for hitting a HTML::a tag with src=http + HTML::img
int UNKNOWN_TYPE Default HitTestResult, where the target is unknown
I think you will be able to get all events using WebView's setOnTouchListener function.
WebView has inner class named HitTestResult.
HitTestResult class will help us to find the HTML element which press when user click on WebView.
HitTestResult class has only two method.
getExtra() : It return String. String has HTML element which is clicked by user
getType() : It return integer. It is used to identify which HTML element is clicked by user.
You can do like :
wv.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
WebView.HitTestResult hr = ((WebView)v).getHitTestResult();
Log.i(TAG, "getExtra = "+ hr.getExtra() + "\t\t Type=" + hr.getType());
return false;
}
});
Edited :
Refer for perfect answer :
Detect click on HTML button through javascript in Android WebView
In my game, I'd like to have a kind of "message of the day" feature. Basically, when the user taps the "message of the day" button in the main menu, it would open up a browser view in-game. When the user is done, he taps a "close" button and the view disappears, returning him to the game menu.
So, is it possible to create a browser view dynamically? If yes, how?
The following can be used in a Dialog as well
public void setWebView() {
//WebViewClient is used if you want to capture stuff from the webview, like if a link was pressed
WebViewClient yourWebClient = new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
open_web = true;
if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = "http://" + url;
}
Intent browserIntent = new Intent("android.intent.action.VIEW",
Uri.parse(url));
startActivity(browserIntent);
return true;
}
};
WebView wv = (WebView) findViewById(R.id.webview);
wv.setWebViewClient(yourWebClient);
String str = getHtml();
wv.loadData(str, "text/html", "utf-8");
wv.setBackgroundColor(0);
}
boolean isAsset = false;
private String getHtml(){
InputStream source = null;
if (isAsset){
source = getAssets().open("motd.html");
} else {
source = new FileInputStream(Environment.getExternalStorageDirectory() + "motd.html");
}
}
You could probably just use a WebView. When you want it to show up you could just set its view state to View.VISIBLE. When you want it to go away, just set its view state to View.GONE.
You can add/remove a WebView to your game dynamically (or show/hide it, whatever you prefer).
Take a look at the WebView tutorial for more info:
http://developer.android.com/resources/tutorials/views/hello-webview.html
Make sure to leave room for your "Close" button in your layout, i.e. you don't want to set the layout height to "fill_parent".