import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
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.web_view);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new CustomWebViewClient());
mWebView.loadUrl("http://groupedirectouest.com");
}
}
class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
when I use ua (userAgent) string, webview shows desktop version of website.
here is the ua string i have used :
mWebView.getSettings().setUserAgentString("AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30");
here is the website that i am trying to open in my
http://groupedirectouest.com/
it works fine with chrome but webview.
Now there are custom chrome tabs for you!
The WebView is good solution if you are hosting your own content inside your app. If your app directs people to URLs outside your domain,it is now recommended that you use Chrome custom tabs for various benefits.Follow the link below:
https://developer.chrome.com/multidevice/android/customtabs
Related
I already make a java WebView for my website, this website in the footer the phone number and the email address when I click in it they told me (the webpage at tel could not be loaded because err_unknown_url_scheme), What should i do ?
MainActivity.Java
package xxx.xxxxxxx.xxxxxxxxx;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import java.util.Objects;
public class MainActivity extends AppCompatActivity {
private WebView webView;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try
{
Objects.requireNonNull(this.getSupportActionBar()).hide();
}
catch (NullPointerException ignored){}
//find the view
webView = findViewById(R.id.webview);
// to load all the web page in app itself use this
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://www.ezyadvs.com");
WebSettings webSettings =webView.getSettings();
//if your website is using any javascript that needs to load some script then you need to enable javascript in android application
webSettings.setJavaScriptEnabled(true);
}
#Override
public void onBackPressed() {
// if any previous webpage exist then onback pressed will take care of it
if(webView.canGoBack())
{
webView.goBack();
}else{
//else it will exit the application
super.onBackPressed();
}
}
}
Here is my solution in .kt (kotlin) :
this.webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
//add condition startsWith("tel:") or startsWith("mailto:")
if (url!!.startsWith("whatsapp:")) {
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse(url)
startActivity(intent)
return true
}
return false
}
}
I have an app with a webview. The webview links to a mobile webpage where all links work correctly. When I click on a link in this webview, the link opens up in google maps inside the webview. Instead, I want the link to open up the native google maps app. Can anyone tell me how to do this?
I have this code that I need to insert it to my code
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("geo:")) {
Uri gmmIntentUri = Uri.parse(url);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent);
}
return true;
}
view.loadUrl(url);
return true;
}
This is my code:
package com.company.test2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView) findViewById(R.id.webView);
webView.loadUrl("www.google.com");
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
}
}
To call android method from javascript you would need to make Javascriptinterface class and annotating method like #JavascriptInterface in documentation
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
#JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
and bind it to webview like
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
and from javascript you can call it like
function showtoastfromsite(){
Android.showToast("hello from website");
}
I have my webpage (CYNX.ML/U1.html) which displays perfectly in all other browsers, However I am using WebView to display it to the users which seems to shown it broken.
This is how it appears on WebView- http://prntscr.com/irhosm
Any way to fix this? It seems to be displayed perfectly on all mobile web browsers- Including Chrome, Dolphin Web browser, UC web browser
*CODE of MainActivity.java-
package com.sirseni.simpleandroidwebviewexample;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.myWebView);
myWebView.loadUrl("http://cynx.ml/U1.html");
myWebView.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUserAgentString("Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3");
}
// Use When the user clicks a link from a web page in your WebView
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.centerend.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
**UPDATE:**Had to inform that I feel WebView is having slight problems with the page's css styling, however Google seems to show it as 100% mobile compatible. Any ideas?
I'm a beginner on this kinda stuff.
I have a button that when I click on it, it loads a page on webview.
The code is the following:
Right now when I'm clicking on the button, the page doesn't load.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
public class WebActivity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web2);
String url = "http://giae.aeourem.pt";
WebView web = (WebView) findViewById(R.id.webview2);
web.loadUrl(url);
}
}
I have a simple webview which loads a page. This page has a few links that opens within the webview. That's what it supposed to do, so it's working all fine.
But there is one single link from that page which should load as a popup, so I want it to open in the normal browser when people click it. But as I stated, all links are opening in the webview, so that link does it also.
My question is, how can I make this link open in the normal browser as a kind of a popup? Is it even possible? The link is variable so it's changing always, it cannot be hardcoded within the application to open in a new browser browser.
Is it possible and how can I do it?
Here's an example of overriding webview loading to stay within your webview or to leave:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class TestWebViewActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new MyWebViewClient());
}
}
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("somePartOfYourUniqueUrl")){ // Could be cleverer and use a regex
return super.shouldOverrideUrlLoading(view, url); // Leave webview and use browser
} else {
view.loadUrl(url); // Stay within this webview and load url
return true;
}
}
}
public class WebViewActivity extends Activity {
private WebView webView;
private ProgressDialog progress;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.loadUrl("https://www.example.com");
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("https://www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
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));
startActivity(intent);
return true;
}
}
}