Prevent Double Tap on Android WebView - java

I have a fullscreen WebView and I want to;
If my user double tap the screen, prevent second tap.
I am very very new on Android, please be very clear. I pasted my codes;
MainActivity.java
public class MainActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// REMOTE RESOURCE
mWebView.loadUrl("https://www.example.com");
mWebView.setWebViewClient(new MyWebViewClient());
}
// Prevent the back-button from closing the app
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
}
MyWebViewClient.java
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().endsWith("www.example.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}

Related

pass a link in a webview

I am trying when someone click for example : eee.com , to open it in my Webview app
public class MainActivity extends AppCompatActivity {
public static WebView webView;
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.mainWebview);
webView.setWebViewClient(new WebViewClient());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("https://www.eee.com/");
webView.setWebViewClient(new WebViewClient(){
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("eee.com")) {
view.loadUrl(url);
} else {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
return true;
}
});
}
#Override
protected void onResume() {
super.onResume();
Uri uri = this.getIntent().getData();
webView.loadUrl(uri.toString());
}
but the app crashes on open it right away , I tried this : :
#Override
protected void onResume() {
super.onResume();
Uri uri = this.getIntent().getData();
if(getIntent().getData() != null){
webView.loadUrl(uri.toString());
}
}
app works but on link click it just open the app without passing the link
I solved it my self with :
#Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
Uri x = intent.getData();
webView.loadUrl(x.toString());
}

Webview set overrideurl not in use

Im trying to use the shouldOverrideUrlLoading to handle some tel: links on my webview, but i get this error:
error: method does not override or implement a method from a supertype
How can i fix that?
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.WebView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("https://**************/");
myWebView.setWebViewClient(new WebViewClient());
final ProgressBar Pbar;
Pbar = (ProgressBar) findViewById(R.id.pB1);
myWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
if(progress < 100 && Pbar.getVisibility() == ProgressBar.GONE){
Pbar.setVisibility(ProgressBar.VISIBLE);
}
Pbar.setProgress(progress);
if(progress == 100) {
Pbar.setVisibility(ProgressBar.GONE);
myWebView.requestFocus(View.FOCUS_DOWN);
}
}
#SuppressWarnings("deprecated")
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
view.reload();
return true;
}
view.loadUrl(url);
return true;
}
});
}
}
the override is grayed out but i cant figure how to fix it, im not very familiar with java and its driving me crazy, thanks in advance!
This is because there is no shouldOverrideUrlLoading in the WebChromeClient that you are trying to override.
You can override the shouldOverrideUrlLoading of WebChromeClient as it is present in it

How to fix 'cannot find symbol variable MyWebViewClient'

I'm getting error: cannot find symbol variable MyWebViewClient.
public class MainActivity extends AppCompatActivity {
WebView myWebView = (WebView) findViewById(R.id.webview);
#SuppressLint("JavascriptInterface")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView.loadUrl("https://*********.com"); //Just removed for security
WebSettings ws = myWebView.getSettings();
ws.setJavaScriptEnabled(true);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(MyWebViewClient);
WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
}
#Override
public void onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack();
}
else{
super.onBackPressed();
}
}
}
and my MyWebViewClient class
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("https://*********.com")) {
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
return true;
}
}
(Imported all necessary header files as instructed by Android-studio)
But I'm getting error: cannot find symbol variable MyWebViewClient.
Please someone help me.
It looks like you are trying to pass your MyWebViewClient class as an instance in this line :
myWebView.setWebViewClient(MyWebViewClient);
maybe try something like this :
myWebView.setWebViewClient(new MyWebViewClient());
or
MyWebViewClient myWebviewClientInstance = new MyWebViewClient();
myWebView.setWebViewClient(myWebviewClientInstance);

onBackPressed() crashes my webview app

I am new to android development, I have tried to override onBackPress() to implement webView.GoBack(). But on pressing back key my apps getting crashed. Here is my MainActivity.java code. Am I doing something wrong ??
public class MainActivity extends AppCompatActivity {
private WebView webview;
/** Called when the activity is first created. */
public void onBackPressed (){
if (webview.isFocused() && webview.canGoBack()) {
webview.goBack();
}
else {
super.onBackPressed();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView view = (WebView) findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new MyCustomWebViewClient());
view.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
view.loadUrl("http://url");
}
private class MyCustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
//show webview
findViewById(R.id.webView).setVisibility(View.VISIBLE);
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
if (view.canGoBack()) {
view.goBack();
} else {
view.loadUrl("file:///android_asset/index.html");
}
Toast.makeText(getBaseContext(), description, Toast.LENGTH_LONG).show();
}
}
}
Just because your webView is null. You are not referencing it anywhere. Modified your code.
public class MainActivity extends AppCompatActivity {
private WebView view;
/** Called when the activity is first created. */
public void onBackPressed (){
if (view.isFocused() && view.canGoBack()) {
view.goBack();
}
else {
super.onBackPressed();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (WebView) findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new MyCustomWebViewClient());
view.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
view.loadUrl("http://url");
}
private class MyCustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
//show webview
findViewById(R.id.webView).setVisibility(View.VISIBLE);
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
if (view.canGoBack()) {
view.goBack();
} else {
view.loadUrl("file:///android_asset/index.html");
}
Toast.makeText(getBaseContext(), description, Toast.LENGTH_LONG).show();
}
}
}
In onCreate you have:
WebView view = (WebView) findViewById(R.id.webView);
The WebView you have defined at the top has not been initialized, and is the one being used by the onBackPressed method, to fix this all you need to do is remove WebView before initializing the variable in onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (WebView) findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new MyCustomWebViewClient());
view.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
view.loadUrl("http://url");
}
Actually you can not do directly inside the fragment. The onBackPressed can be overridden in the FragmentActivity. What you can do is:
1.Override the onBackPressed inside the activity.
2.When the onBackPressed is called, check if the instance of the current fragment is the instance showing the webview.
3.If it is, ask the fragment if the webview can go back.
4.If it is not, call the super or whatever you need
#Override
public void onBackPressed() {
Fragment webview = getSupportFragmentManager().findFragmentByTag("webview");
if (webview instanceof MyWebViewFragment) {
boolean goback = ((MyWebViewFragment)webview).canGoBack();
if (!goback)
super.onBackPressed();
}
}

onBackPressed() - cannot resolve symbol 'webView'

I'm trying to get back button functionality in my WebView app, but the onBackPressed() function is giving the error 'cannot resolve symbol webView'
Here's my code:
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdView mAdView;
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
WebView webView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
// To keep browser in width of phone
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
// Sets default text size
webSettings.setTextZoom(175);
MyWebViewClient webViewClient = new MyWebViewClient();
webView.setWebViewClient(webViewClient);
webView.loadUrl("http://www.google.com");
}
#Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
You can do this in onBackPressed:
#Override
public void onBackPressed() {
WebView webView = (WebView) findViewById(R.id.webview);
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
Edit:
You can do this (with field):
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState){
// ...
this.webView = (WebView) findViewById(R.id.webView);
// ...
}
#Override
public void onBackPressed(){
// now you don't need findViewById because you can access field prepared in onCreate
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}

Categories