WebView Android Studio - java

I have created a WebView of this website https://smartfix.pk/ but in my app, WhatsApp is not working,
when I click it gives me the error
webpage at whatsapp://send/?phone=923005475032&text=Hello%2C%20I%20have%20a%20question%20about%20SmartFix%20Services could not be loaded because: net::ERR_UNKNOWN_URL_SCHEME
public class MainActivity extends AppCompatActivity {
WebView web;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = findViewById(R.id.webView);
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
web.setWebViewClient(new Callback());
web.loadUrl("https://smartfix.pk/");
}
private class Callback extends WebViewClient {
#Override
public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
return false;
}
}
}

try to add this, i'm not sure it's will work for that page but when i'm using webview i always enable all to be sure
webSettings.setLoadsImagesAutomatically(true);
webSettings.setDatabaseEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
}

Related

Android Webview app shows black screen when i open it

Android weview app I compiled in andriod studio crashes immediately with a blackscreen when I open it. The app uses an iframe embed url.Compiles fine but crashes with a black screen when I open it.
Why is it crashing?
public class MainActivity extends AppCompatActivity {
private WebView zwebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String Video;
Video = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/eGPbD-f0b_A\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>";
WebSettings webSettings = zwebView.getSettings();
webSettings.setJavaScriptEnabled(true);
zwebView.setInitialScale(1);
zwebView.getSettings().setAllowFileAccess(true);
zwebView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
zwebView.setWebChromeClient(new WebChromeClient());
zwebView.getSettings().setJavaScriptEnabled(true);
zwebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
zwebView.getSettings().setLoadWithOverviewMode(true);
zwebView.getSettings().setUseWideViewPort(true);
zwebView.getSettings().setAppCacheEnabled(true);
zwebView.loadData(Video, "text/html", "utf-8");
zwebView = (WebView) findViewById(R.id.myWebView);
zwebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request) {
webView.loadUrl(request.getUrl().toString());
return true;
}
});
zwebView.loadUrl(Video);
I just copy-paste your code and its working fine the video is loading in the webview so if you face issue can you kindly share the log details that what error you actually getting.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView zwebView = new WebView(this);
String Video;
Video = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/eGPbD-f0b_A\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>";
WebSettings webSettings = zwebView.getSettings();
webSettings.setJavaScriptEnabled(true);
zwebView.setInitialScale(1);
zwebView.getSettings().setAllowFileAccess(true);
zwebView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
zwebView.setWebChromeClient(new WebChromeClient());
zwebView.getSettings().setJavaScriptEnabled(true);
zwebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
zwebView.getSettings().setLoadWithOverviewMode(true);
zwebView.getSettings().setUseWideViewPort(true);
zwebView.getSettings().setAppCacheEnabled(true);
zwebView.loadData(Video, "text/html", "utf-8");
zwebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request) {
webView.loadUrl(request.getUrl().toString());
return true;
}
});
zwebView.loadUrl(Video);
setContentView(zwebView);
}
}
Here is the code which I use and it's working. not crashing after all.

How to access .aspx pages in android WebView?

I'm doing a website to Android app,but .aspx page to webview is not loading, I got white screen.
MainActivity
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv = (WebView) findViewById(R.id.webview);
// Enable Javascript
WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);
wv.setWebChromeClient(new WebChromeClient());
wv.setWebViewClient(new myWebViewClient());
String url = "https://something.aspx";
wv.loadUrl(url);
}
}
myWebViewClient
class myWebViewClient extends WebViewClient {
}
}
How to load .aspx page in android WebView please solve my issue,
Thanks in advance.
Add the code below in your myWebViewClient class:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}

Android Studio - WebView - open internal links inside WebView, external in default browser

I am trying to add an WebView to my app, but would like to allow to open inside my app only links which are connected with my domain (www.example.com). All other links which doesn't contain the name of my domain (example.com) should be opened externaly by default browser. Using information from stockoverflow I have prepared the following code, but it still doesn't work and all links are opened inside my app, despite shouldOverrideUrlLoading is created ("if (Uri.parse(url).getHost().contains("example.com"))"). Is anyone able to help to resolve this problem? I was trying a lot of samples, but still cannot find the way how to fix it, but I fell that it can be just a small mistake in the code.
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.www);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.example.com");}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().contains("example.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
Resolved! I am not sure that the code is 100% correct, but works fine and maybe it will be useful for someone (I am not the author, I just used a few samples from stackoverflow and put them together):
public class MainActivity extends AppCompatActivity {
private WebView webView;
private ProgressDialog progress;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.www);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.loadUrl("https://www.example.uk");
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().contains("example.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}

Change text of the edit text by html

How can I do that?
public class ActivityTest extends Activity {
public EditText edtText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
WebView webView = (WebView) findViewById(R.id.webView);
webView.loadUrl("file:///android_res/raw/htmlimages.html");
webView.addJavascriptInterface(new MyTest(), "Scripts");
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
edtText = (EditText) findViewById(R.id.edtText22);
you can use fromHtml method Like this :
String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText));
In your case to change EditText call this.edtText.setText("What you want...")

Remove banners in the webview?

Is possible remove the banners in a webview? In my webview i display a forum and there is an annoying banner i want to remove.. Is it possible? Is there a way like adBlock or something like that? My code:
public class MainActivity extends Activity
{
protected WebView webview;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
WebSettings websettings = webview.getSettings();
websettings.setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient());
if (savedInstanceState == null)
{
webview.loadUrl("http://www.proboards.com/");
}
}
#Override
protected void onSavedInstanceState(Bundle outState) {
//super.onSavedInstanceState(outState);
webview.saveState(outState);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if(webview.canGoBack() == true)
webview.goBack();
else finish();
break;
default:
break;
}
return super.onKeyDown(keyCode, event);
}
}
Thanks.
If you want to hide a banner in a specific page, look at the page's source for the banner's id and inject some javascript code (example here) in order to set its visibility to hidden.

Categories