i have web view and i try to give the user button to refresh but i cant set that button i try to use this code
the refresher button
imgbtr2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ImageButton imgbtr2 = (ImageButton) findViewById(R.id.imageButtonr2);
WebView webView = (WebView)findViewById(R.id.webView);
webView.loadUrl("javascript:window.location.reload(true)"); // TODO Auto-generated method stub
}
});
the webview
webView = (WebView) findViewById(R.id.webView1);
webView.loadUrl("http://m.facebook.com/");
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
but it seems like javascript:window.location.reload(true) its not work . if i use webView.loadUrl("www.exapml.com"); it will work but this is not last Url u was there not reopen the webpage from the first url
and why webview can not open page like https://vine.co/ is it coz its not end with .com .. and how to make it open all the website .. and when u enter website like Myspace and try to login using Facebook everything will work ok until u finish login it will not show anything after u connect it to ur facebook ,, is it coz of the redirection and if it is how to fix it
this is the last 3 thing's stop me from finishing my app . i hope someone can help
Use web.reload(); method to do so.
imgbtr2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
web.reload();
}
});
Call again
mWebView.loadUrl("http://www.facebook.com");
so the full code would be
newButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dgeActivity.this.mWebView.loadUrl("http://www.facebook.com");
}});
Related
I developed an android webview application, and everything is workong fine. But inside the website you can login via a facebook button, which is working in all browsers, but not inside the webview app. Just when I click the facebook login button a blank page appears.
Others have quite similar problems like me, but they can load the facebook page instead of me.
I also tried it with these adds:
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
and
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().setAcceptThirdPartyCookies(myWebView, true);
}
I also want it to open internal, and then redirect automatically to my website's login page.
EDIT:
Maybe I should also say that the facebook login page opens with a popup and that seems to be a problem.
Try to change the your code same as following
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView htmlWebView = (WebView)findViewById(R.id.webView);
htmlWebView.setWebViewClient(new CustomWebViewClient());
WebSettings webSetting = htmlWebView.getSettings();
webSetting.setJavaScriptEnabled(true);
webSetting.setDisplayZoomControls(true);
htmlWebView.loadUrl("https://inducesmile.com/blog");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class CustomWebViewClient extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
Try something like below code. You will get error response and you will get to know about the problem.
private void load webPage(){
private WebView mWebview=null ;
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(getApplicationContext(), description, Toast.LENGTH_SHORT).show();
}
});
mWebview.loadUrl("YOUR URL");
setContentView(mWebview );
}
}
We just changed the facebook login site, it wont now appear inside a pop-up. Bute theres is still a small problem, afte you login in facebook it brings you back to the main login site. It seems that the login wasnt successfull, but it is. you have to go to the main site for example to see that you are logged in. Maybe someone knows why?
Using Android Studio I am trying to set the value of a textbox in a login form of a web page which is not mine.
Here's the webView i use to work on the page:
final WebView webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
webView.setWebViewClient(new WebViewClient()
{
#Override
public void onPageFinished(WebView view, String url)
{
webView.loadUrl("javascript: document.getElementById('j_password').value = 'something'");
}
});
webView.loadUrl("http://sg18224.scuolanext.info/");
the problem is that when the page has finished loading instead of setting the textbox value, the whole page becomes white and it only displays the string i was trying to set as value (in this case "something").
If i include the js line in an alert like this:
webView.loadUrl("javascript: alert(document.getElementById('j_password').value = 'something')");
it works! But also displays an alert i don't want to display.
This should help you:
final String js = "javascript:document.getElementById('j_password').value='something';";
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if (Build.VERSION.SDK_INT >= 19) {
view.evaluateJavascript(js, new ValueCallback<String>() {
#Override
public void onReceiveValue(String s) { }
});
} else {
view.loadUrl(js);
}
}
});
I am not sure with this answer but you may try it. typically it should be the answer. Long time no interaction with javascript though.
you may try
webView.loadUrl("javascript: document.getElementById('j_password').innerHTML = 'something'");
please comment if its not working.
I have occurred a problem. In my app,I load a url(a php page) by webview. It runs fun in many devices.But in a TV device,it offen exit without any error log.Now I know it is because the php page load a swf flash.But the page show well in the browser in the Tv.My code is as follows:
mWebView = (WebView) v.findViewById(R.id.shopWebView);
mWebView.setOnKeyListener(this);
mWebView.setInitialScale(100);
WebSettings webSet = mWebView.getSettings();
webSet.setJavaScriptEnabled(true);
webSet.setBlockNetworkImage(true);
webSet.setSupportZoom(true);
webSet.setBuiltInZoomControls(true);
webSet.setSupportMultipleWindows(true);
webSet.setUseWideViewPort(true);
webSet.setLoadWithOverviewMode(true);
// webSet.setPluginState(PluginState.ON);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
view.getSettings().setBlockNetworkImage(false);
}
});
mWebView.loadUrl(URL_SHOP);
Who can tell me what's wrong with my code or the reason of this case?(Forgive my poor English.)
Use WebChromeClient. That May help.
webView.setWebViewClient(new MyWebClient());
public class MyWebClient extends WebChromeClient {
}
I have solved this problem.I did like this:Before webview load url,I set the webview gone;and set webview visible in onPageFinished().Although I have solved the problem,but I don't know the exactly reason of that condition.If anyone know,please tell me.Is it because the TV's vedio memory or other things like that?
So, reading around i read that i could "inject" javascript into a loaded webpage to programatically fill a website's form, so i tried the following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView WebView;
WebView = (WebView) findViewById(R.id.webview);
WebView.setWebViewClient(new HelloWebViewClient());
WebView.getSettings().setJavaScriptEnabled(true);
WebView.getSettings().setBlockNetworkImage(false);
WebView.loadUrl("http://www.kingsage.es");
String username = "XXX";
String Password = "YYY";
WebView.loadUrl("javascript:document.getElementById('login_player').value='"+username+"';javascript:document.getElementById('login_passwd').value = '"+Password+"';");
}
However, when i run this on the emulator i get an error as if the website could not be found. if i turn off the second loadUrl method i load the website, but i need to fill the login form programatically!, i figure i am doing something wrong but i haven't found and answer reading around. Any help would be appreciated!!
you need to wait until the webpage is loaded (use WebViewClient) then execute your javascript
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished (WebView webView, String url)
{
webView.loadUrl("javascript:document.getElementById('login_player').value='"+username+"';javascript:document.getElementById('login_passwd').value = '"+Password+"';");
}
});
make also sure that you execute only once, not on every onPageFinished Event
I like JeanLuc's suggestion and it worked for me 98% of the way. I still needed to automatically log the user into the web page once their username and password had been entered so I did the following and its worked well, except for trying to remove the "do you want to remember the password' message.
#Override
public void onPageFinished (WebView webView, String url)
{
webView.loadUrl("javascript:document.getElementById('Login1_UserName').value='"+userName+"';javascript:document.getElementById('Login1_Password').value = '"+password+"';");
webView.loadUrl("javascript:document.getElementById('Login1_LoginButton').click();");
}
The second line selected the 'login' button and logged them in automatically. Hope it helps someone else.
Here is how I acheived it in my scenario where I had to browse a page, fill text boxes and submit.
WebView webView = FindViewById<WebView>(Resource.Id.webEspView);
webView.SetWebViewClient(new Client());
webView.Settings.JavaScriptEnabled = true;
webView.LoadUrl("http://192.168.4.1")
Then added the Client class and overloaded onPageFinished() method.
public class Client : WebViewClient
{
public override void OnPageFinished(WebView view, string url)
{
base.OnPageFinished(view, url);
string ssid = Variables.GetInitialUsername();
ssid = ssid.Trim(new char[] {'\"'});
string pass = Variables.GetInitialPassword();
var script = $"document.getElementById(\"clientId\").value = \"{ssid}\";" +
$"document.getElementById(\"branchId\").value = \"{pass}\";" +
"document.querySelector('button[type=\"submit\"]').click()";
view.LoadUrl($"javascript: {script}");
}
}
Hope it helps.
In my application I am using WebView and in that I am using JavaScript alert( ) method but its not working, no pop-up appears.
in my manifest file I have added
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
and in activity file I have added
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/demo.html");
In layout xml file I have added
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Any clue how to enable full JavaScript in WebView?
Update
Thanks mark
the alert() method in the html file are working now :) .
Now there are two issues in WebView :
1: I am using a <textarea> in the html file that i am loading in WebView , and trying to write in Hindi language font in it, but when i try to write Hindi text it displays as symbols ( rectangle symbols like [] ) .
when i do the same in firefox browser on desktop it works fine.
any clue how to give support for multiple language in textarea in WebView ?
2: When I am clicking submit and trying to open the value of text in alert() method in another java script it doesn't work , does it mean even after using WebChromeClient
its applicable only for current loaded html page and not javascripts called from that page ?
As others indicated, setting the WebChromeClient is needed to get alert() to work. It's sufficient to just set the default WebChromeClient():
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebChromeClient(new WebChromeClient());
Thanks for all the comments below. Including John Smith's who indicated that you needed to enable JavaScript.
Check this link , and last comment , You have to use WebChromeClient for your purpose.
webView.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
return super.onJsAlert(view, url, message, result);
}
});
The following code will work:
private WebView mWebView;
final Activity activity = this;
// private Button b;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setProgress(progress * 1000);
}
});
mWebView.loadUrl("file:///android_asset/raw/NewFile1.html");
}
You can try with this, it worked for me
WebView wb_previewSurvey=new WebView(this);
wb_previewSurvey.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
//Required functionality here
return super.onJsAlert(view, url, message, result);
}
});
if you wish to hide URL from the user, Show an AlertDialog as below.
myWebView.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Log.d(TAG, "onJsAlert url: " + url + "; message: " + message);
AlertDialog.Builder builder = new AlertDialog.Builder(
mContext);
builder.setMessage(message)
.setNeutralButton("OK", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int arg1) {
dialog.dismiss();
}
}).show();
result.cancel();
return true;
}
}