I am making an app for WebView with a header progress bar. It is working nicely but after loading the web page, if I click any link on the webpage, the app closes and opens the default browser. But instead, I want to stay in the WebView page after the click and open the new tab on the same page.
How can I do this?
public class MainActivity extends Activity {
private WebView webView;
private EditText urlEditText;
private ProgressBar progress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// urlEditText = (EditText) findViewById(R.id.urlField);
webView = (WebView) findViewById(R.id.webView);
//webView = new WebView(this);
progress = (ProgressBar) findViewById(R.id.progressBar);
progress.setMax(100);
webView.setWebChromeClient(new MyWebViewClient());
//Button openUrl = (Button) findViewById(R.id.goButton);
String url = "http://amrtube.com/result/index.php";
if (validateUrl(url)) {
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptEnabled(true); // enable javascript
this.webView.getSettings().setAllowFileAccess(true);
this.webView.getSettings().setBuiltInZoomControls(true);
//this.mWebview.getSettings().setSupportZoom(true);
final Activity activity = this;
this.webView.setScrollBarStyle(33554432);
this.webView.setScrollbarFadingEnabled(false);
webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.loadUrl(url);
MainActivity.this.progress.setProgress(0);
}
}
private boolean validateUrl(String url) {
return true;
}
private class MyWebViewClient extends WebChromeClient {
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.toString());
return true;
}
#Override
public void onProgressChanged(WebView view, int newProgress) {
MainActivity.this.setValue(newProgress);
super.onProgressChanged(view, newProgress);
}
}
public void setValue(int progress) {
this.progress.setProgress(progress);
}
}
Replace your line with this
this.mWebView.setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);
Related
I try to open the dialer while clicking on call button in post, but it is not working. All code is running but only this part not working.
I don't want to change the file uploading code, I only need to change call button code in webview:
public class MainActivity extends AppCompatActivity {
WebView webView;
private static final String TEL_PREFIX = "tel:";
public String url ="https://www.negotiatar.com";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient());
webView.setInitialScale(0);
webView.setWebViewClient(new CustomWebViewClient());
private class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if(url.startsWith(TEL_PREFIX)) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
}
}
you have a big mistake in your code it seems you have nested class inside the method, are you able to do this like you have made?
I'm trying to close the ad popup window on webview onBackPressed. But it keeps reloading the ad window. I am using exoclick and juicyads popunders on the website.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
WebView view = findViewById(R.id.webview);
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
view.setSystemUiVisibility(uiOptions);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setDomStorageEnabled(true);
view.getSettings().setSupportMultipleWindows(true);
view.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
//view.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
view.setWebViewClient(new WebViewClient() {
view.loadUrl(url);
return false;
});
view.loadUrl("https://example.com");
}
#Override
public void onBackPressed() {
WebView view = findViewById(R.id.webview);
if (view.canGoBack()) {
view.goBack();
return;
} else {
moveTaskToBack(true);
}
}
}
I have a Web View in Android,and I open a html web page in it.But it's full of links and images,and when I click one of them,it loads in my webview. So I want to disable this behavior,so if I click on a link,don't load it. I've tried this solution and edited a bit for myself, but not worked.
Please give me a solution with full code. I'm new to Java and Android.
My Full code here:
public class MainActivity extends AppCompatActivity {
ProgressBar progressBar;
WebView webView;
String url="http://example.com";
TextView textView;
//to hide progressbar after loading part 1
LinearLayout liProgressContainer;
private String currentUrl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
webView = (WebView) findViewById(R.id.webView);
textView = (TextView) findViewById(R.id.tvLoadingPercentage);
//to hide progressbar after loading part 2
liProgressContainer = (LinearLayout) findViewById(R.id.liProgressContainer);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
textView.setText(progress + " %");
}
});
webView.setWebViewClient(new MyWebViewClient());
WebSettings browserSetting = webView.getSettings();
browserSetting.setJavaScriptEnabled(true);
webView.loadUrl(url);
}
//back button function
#Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
liProgressContainer.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
//return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
liProgressContainer.setVisibility(View.GONE);
//hide header part
}
}
}
You need to configure your shouldOverrideUrlLoading method in your MyWebViewClient class to only load a URL should it match your desired URL. Add a field to your MyWebViewClient class to store a URL and add a single-arg constructor.
private String desiredUrl;
public MyWebViewClient(String url) {
this.desiredUrl = url;
}
In your shouldOverrideUrlLoading method, check to see if the supplied URL is equal to your URL instance variable.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (desiredUrl.equals(url))
view.loadUrl(url);
return true;
}
When setting your web view client, pass your URL argument, e.g.
webView.setWebViewClient(new MyWebViewClient(url));
I'm trying to develop some simple app based on WebView, and make possible to exchange data between android app and remote page.
Problem description:
loadUrl works within onCreate, but not working from outside of onCreate. Please see code below and explain what I doing wrong, why it's not working within WebAppInterface class:
public class MainActivity extends Activity {
public WebView view1;
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "htxx://mydomain.com/";
final WebView view1 =(WebView) this.findViewById(R.id.webView);
view1.addJavascriptInterface(new WebAppInterface(this), "Android"); //exchange between web-js and webview
WebSettings webSettings = view1.getSettings();
webSettings.setJavaScriptEnabled(true);
view1.loadUrl(url);
view1.setWebViewClient(new com.example.project.MyAppWebViewClient() {
#Override
public void onPageFinished(WebView view1, String url) {
findViewById(R.id.progressBar1).setVisibility(View.GONE);
findViewById(R.id.webView).setVisibility(View.VISIBLE);
}
});
view1.setInitialScale(100);
///// ---> That's works:
view1.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
view1.loadUrl("javascript:somescript('Page is loaded')");
}
});
///// ---> That's works too:
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String device_id = tm.getDeviceId();
view1.loadUrl("javascript:somescript('"+device_id+"')");
}
});
}
public class WebAppInterface {
Context mContext;
WebAppInterface(Context c) {
mContext = c;
}
#JavascriptInterface
public void showToast(String toast) {
//show toast from web-app (js to android)
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
#JavascriptInterface
public void printFromWeb() {
//show toast from web-app (js to android)
///// ---> That's NOT working :(
view1.loadUrl("javascript:somescript('Checking loadurl from outside of onCreate')");
}
}
}
Interesting, that not any errors occurs in Android studio.
You have public WebView view1; as a field and completely different final view1 inside onCreate(), so it's actually null in your public void printFromWeb() method.
Here is the safe route to go:
To access WebView "from outside", add a private field of WebView class:
private WebView webView;
In your onCreate() method, instantiate it:
webView = (WebView) findViewById(R.id.webView);
Add a getter method to access the instance:
public WebView getWebView() {
return webView;
}
And remember to only touch it only on UI thread.
Update: how to safely access views from not-ui thread:
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run(){
view1.loadUrl(...)
}
})
I want the user to input their search query into a search widget and then have the search results displayed to them and then when they click on a url that they want, I need to be able to save that url for use in other parts of my application.
Is this possible, and if not what are some of the restrictions preventing this?
EDIT -- I figured I'd add the code for my activity that has the webview.
public class State extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitleSupport = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.state);
if( customTitleSupport ){
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title );
}
final TextView myTitleText = (TextView)findViewById(R.id.my_title);
if( myTitleText != null ){
myTitleText.setText(R.string.app_name);
}
WebView stateBrowser = (WebView)findViewById(R.id.state_search);
stateBrowser.loadUrl("http://www.google.com/");
}
}
you need to use something called WebViewClient
public class WebViewTestActivity extends Activity implements
View.OnClickListener {
/** Called when the activity is first created. */
Button btn = null;
WebView myWebView =null;
EditText et =null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(this);
et = (EditText) findViewById(R.id.editText1);
myWebView = (WebView) findViewById(R.id.webview);
// WebViewClient in use.
myWebView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
et.setText(url);
}
});
}
#Override
public void onClick(View source) {
if (btn.getText().equals("Go!")) {
myWebView.loadUrl(et.getText().toString());
}
}
}
`