Starting intent in an extra WebView class instead in an inner class? - java

I wanted to keep my MainActivity small and clear. So I have created an seperate class for WebView instead of an inner class this way:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
mWebView.setWebViewClient(new CustomWebViewClient());
But now I wanted to start a new activity in CustomWebViewClient with the openInAppBrowser method:
public class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith(PROJECT_REMOTE)) {
return false;
} else {
openInAppBrowser(this, url);
}
return true;
}
private void openInAppBrowser(Activity activity, String url) {
Intent intent = new Intent(activity, InAppBrowser.class);
intent.putExtra("url", url);
activity.startActivity(intent);
}
Since this and MainActivity.this won't work here, how could I reference the MainActivity to make this method work? Or is the inner class inside the MainActivity the better or only option?

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
mWebView.setWebViewClient(new CustomWebViewClient(this));
public class CustomWebViewClient extends WebViewClient {
Activity activity;
public CustomWebViewClient (Activity activity) {
this.activity= activity;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith(PROJECT_REMOTE)) {
return false;
} else {
openInAppBrowser(url);
}
return true;
}
private void openInAppBrowser(String url) {
Intent intent = new Intent(activity, InAppBrowser.class);
intent.putExtra("url", url);
activity.startActivity(intent);
}
As requested. I am not sure this will work, that's why I did not put on answer in the first place.

Related

Getting this error when using webview net::ERR_CONTENT_DECODING_FAILED

Using webview on java.
The request I am sending is throwing an error.
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);
mywebView.setWebViewClient(new WebViewClient());
WebView.setWebContentsDebuggingEnabled(true);
mywebView.loadUrl("https://lighthearted-platypus-a4a153.netlify.app/");
WebSettings webSettings=mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
public class mywebClient extends WebViewClient{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
super.onPageStarted(view,url,favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view,String url){
view.loadUrl(url);
return true;
}
}
#Override
public void onBackPressed(){
if(mywebView.canGoBack()) {
mywebView.goBack();
}
else{
super.onBackPressed();
}
}
}
Need to send get request successfully on the api.

Making a call from WebView with Intent is not working

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?

Android webview doesn't open api.whatsapp

I'm trying to open an api.whatsapp link from my webpage using the Android web view but I faced the error ERR_UNKNOWN_URL_SCHEME.
I have changed the shouldOverrideUrlLoading method several times trying to fix it but the intent never works. Have someone faced the same problem before?
This is the current version of my code:
public class MainActivity extends AppCompatActivity {
// public static final String TAG = MainActivity.class.getSimpleName();
private WebView mywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mywebView.getSettings();
mywebView.loadUrl("https://mypage");
webSettings.setJavaScriptEnabled(true);
mywebView.setWebViewClient(new WebViewClient());
}
public class myWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//String url = request.getUrl().toString();
if (Uri.parse(url).getHost().equals("https://mypage")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
Any suggestions?
Add Validating to your shouldOverrideUrlLoading method like:
Patterns.WEB_URL.matcher(url).matches();

Webview loading url but opening content in default browser?

It opens my link in Webview but when I try to open hyperlinks and content of the web or navigation link, it starts opening with the default browser.
public class MainActivity extends Activity {
private WebView mWebView;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = findViewById(R.id.activity_main_webview);
mWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://");
}
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
}
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Objects.requireNonNull(Uri.parse(url).getHost()).endsWith("http://www.ieltsmadeeasy.tk/")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
I want to browse the whole website inside the web view, not any browser please help me
Here is some documentation about WebViews: https://developer.android.com/guide/webapps/webview#java
The problem is, that you don't override the shouldOverrideUrlLoading(view, url) function of the WebViewClient, as you use the default one.
If you use this as the WebViewClient, it should work (copied from examples in documentation):
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 website, 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;
}
}
So, just take this class and change the line mWebView.setWebViewClient(new WebViewClient()); to mWebView.setWebViewClient(new MyWebViewClient());

How to set Progress bar for loading activity with data in android

This is my first class doing Url loading .
public class HelloWebViewClient extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
}
This is my Activity Class where data is displacing in web view.
public class detailedview extends Activity
{
WebView mWebView;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.detailedview);
GetSet gs = new GetSet();
String title = gs.getTitle();
String desc = gs.getDesc();
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setTextSize(TextSize.SMALLER);
mWebView.loadDataWithBaseURL("", "<p align=\"justify\"><b> " + title+"</p></b><p align=\"justify\"><br>"+ desc + "</p></br>", "text/html", "utf-8", "");
mWebView.setWebViewClient(new HelloWebViewClient());
}
}
I want to put progress bar for loading data in my activity, I have tried a lot but I can't do it.
Can you please write code or post code so that I can have a progress bar while loading data according to my code.
add this code to your code
ProgressDialog pd = new ProgressDialog(detailedview .this);
^^^^^^^^^^^^
pd.setMessage("Loading...");
pd.show(); <-----
mWebView.setWebViewClient(new WebViewClient() {
^^^^^^^^
#Override
public void onPageFinished(WebView view, String url) { <-----
super.onPageFinished(view, url);
pd.dismiss(); <-----
}
});
-------------------------------------------------------------------------------
public class detailedview extends Activity
{
WebView mWebView;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.detailedview);
ProgressDialog pd = new ProgressDialog(detailedview .this);
pd.setMessage("Loading...");
pd.show();
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
pd.dismiss();
}
});
GetSet gs = new GetSet();
String title = gs.getTitle();
String desc = gs.getDesc();
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setTextSize(TextSize.SMALLER);
mWebView.loadDataWithBaseURL("", "<p align=\"justify\"><b> " + title+"</p></b><p align=\"justify\"><br>"+ desc + "</p></br>", "text/html", "utf-8", "");
// mWebView.setWebViewClient(new HelloWebViewClient());
}
}
You can use below code
public class Grab extends Activity {
WebView view;
private ProgressDialog progressDialog = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grab);
view = (WebView) findViewById(R.id.WebView01);
view.setBackgroundColor(0); // set the background transparent
progressDialog = ProgressDialog.show(view.getContext(), "Loading...", "Loading...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER );
view.setWebChromeClient(new MyWebChromeClient());
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new MyWebViewClient());
view.loadUrl("http://www.wepware.com/web/apps/jots.do");
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) //override WebViewClient method to open url in activity
{
view.loadUrl(url);
return true;
}
}
private class MyWebChromeClient extends WebChromeClient {
#Override
public void onProgressChanged(WebView view, int newProgress) {
if(progressDialog!=null){
if ( newProgress >=80 ) {
progressDialog.dismiss();
} else {
progressDialog.setMessage(newProgress + " % loaded");
}
}
super.onProgressChanged(view, newProgress);
}
}
}
first make one class say it DownloadWebPageTask
put this in ur oncreate method
GetSet gs = new GetSet();
String title = gs.getTitle();
String desc = gs.getDesc();
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setTextSize(TextSize.SMALLER);
now make an object of that class like this
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute();
Note:**task.execute(); will call ur class "DownloadWebPageTask" u can start this from any line of code write now i have put this in onCreate Method
and here is ur class
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
private final ProgressDialog dialog = new ProgressDialog(Home.this);
// can use UI thread here
protected void onPreExecute() {
this.dialog.setMessage("Loading...");
this.dialog.show();
}
#Override
protected String doInBackground(String... urls) {
String response = "";
mWebView.loadDataWithBaseURL("", "<p align=\"justify\"><b> " + title+"</p></b><p align=\"justify\"><br>"+ desc + "</p></br>", "text/html", "utf-8", "");
return response;
}
#Override
protected void onPostExecute(String result) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
mWebView.setWebViewClient(new HelloWebViewClient());
}
}
Note:**Whenever u use any progress bar than in on post excute method all background process are done till than ur progress bar will be displyed and as soon as data downloading is completed than onPostExcute method will be call.and data downloaded from external server will be stored in webview or u can do any thing with that.
If u still find any query than tell me
Best Of luck
Aamirkhan I.

Categories

Resources