WebView not loading a webpage - android

I am trying to load a simple PTI new page. But getting error. Below is my code:
WebView newsContent = (WebView) findViewById(R.id.news_content);
newsContent.getSettings().setJavaScriptEnabled(true);
newsContent.getSettings().setLoadWithOverviewMode(true);
newsContent.getSettings().setUseWideViewPort(true);
newsContent.loadUrl("http://www.ptinews.com/news/9168439_Aadhaar-linkage-with-bank-accounts-mandatory--says-RBI.html");
And the error is :
EDIT:
I have used the following:
<uses-permission android:name="android.permission.INTERNET" />

Which device you used for checking WebView
I have implement webview as follow and its work for me
activity_webview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.dhruv.demoapiaitutorial.WebVIewActivity">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
WebViewActivity.java
package com.example.dhruv.demoapiaitutorial;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class WebVIewActivity extends AppCompatActivity {
private WebView mWebview;
private ProgressDialog dialog; //creating object of progress dialog
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
dialog = new ProgressDialog(this);
dialog.setMessage("Please wait, your web is being open...");
dialog.setCancelable(false);
mWebview = (WebView) findViewById(R.id.webview);
mWebview.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
/*Showing progress dialog*/
dialog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
/*You can also access url in this overridden method*/
/*closing progress dialog*/
dialog.dismiss();
}
});
mWebview.getSettings().setJavaScriptEnabled(true);
mWebview.getSettings().setLoadWithOverviewMode(true);
mWebview.getSettings().setUseWideViewPort(true);
mWebview.loadUrl("http://www.ptinews.com/news/9168439_Aadhaar-linkage-with-bank-accounts-mandatory--says-RBI.html");
}
}

Related

Android WebView when refresh it always goes to home page

I was try to create a android webview with the refresh option.
Whenever I pull down for refresh it always goes to the home page.
I have try using the swiperefeshlayout. My code are as follows
activity_main.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/destiny"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="#+id/progressBar" />
</RelativeLayout>
MainActivity.java file
package com.my.project;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
SwipeRefreshLayout swipe;
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipe = (SwipeRefreshLayout)findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
WebAction();
}
});
WebAction();
}
public void WebAction(){
myWebView = (WebView)findViewById(R.id.destiny);
progressBar = (ProgressBar)findViewById(R.id.progressBar);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(true);
myWebView.loadUrl("http://myurl");
swipe.setRefreshing(true);
myWebView.setWebViewClient(new WebViewClient());
AppUpdateChecker appUpdateChecker=new AppUpdateChecker(this); //pass the activity in constructure
appUpdateChecker.checkForUpdate(false); //mannual check false here
myWebView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
public void onReceivedError(WebView view, int errorCode, String description, String fallingUrl) {
myWebView.loadUrl("file:///android_asset/error.html");
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
progressBar.setVisibility(View.VISIBLE);
setTitle("Loading.....");
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url){
progressBar.setVisibility(View.GONE);
setTitle(view.getTitle());
super.onPageFinished(view, url);
swipe.setRefreshing(false);
}
});
}
#Override
public void onBackPressed() {
if(myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
}
With this code the refresh always goes to the home page
but I want to refresh the webview on the same page itself.
You are setting all variables and actions again onRefresh() you only need to load current url of WebView again. So you need to change your OnRefreshListener like below;
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
myWebView.loadUrl(myWebView.getUrl());
}
});

Webview progressBar not hiding after page load

I am new with Android developement and I am trying to hide a ProgressBar after page not but it is not hiding.
Here is my code for main_activity.xml and MainActivity.java
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:layout_width="395dp"
android:layout_height="715dp"
android:layout_centerHorizontal="true"
android:id="#+id/webView"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
</WebView>
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_centerInParent="true"
/>
</RelativeLayout >
MainActivity.java
package test.com.webview;
import android.graphics.Bitmap;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
WebView webview;
ProgressBar progressBar;
#Override
public void onBackPressed() {
if (webview.canGoBack()) {
webview.goBack();
} else {
super.onBackPressed();
}
}
#Override
protected void onCreate(#Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview=(WebView) findViewById(R.id.webView);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("www.google.com");
}
public class myWebClient extends WebViewClient{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.INVISIBLE); // to hide
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.VISIBLE); //to show
progressBar.setVisibility(View.INVISIBLE); // to hide
}
}
}
I am using onPageStarted and onPageFinished method for progressbar and tried to get some solution from google but nothing seems working or basically it might be because of my experience with android i am not able to make it work so please help me with my code. Thanks
You have to change this line:
webview.setWebViewClient(new WebViewClient());
to the next line:
webView.setWebViewClient(new myWebClient())
Because you are using default client and not your own that you have created.

Web page not loading on WebView

I'm a beginner in Android Development. So I'm searching online for tutorials to learn about.I got this code that I was trying on my computer, when I run the code and view the App, it says that the webpage is not available,so I added the manifest file, but it still says the same thing
The code is given below:
package com.paresh.webviewclientdemo;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewClientDemoActivity extends Activity {
WebView web;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
web = (WebView) findViewById(R.id.webview01);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("https://www.google.com");
}
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;
}
}
}
layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1" />
</LinearLayout>
Here's the manifest file:
<users-permission android:name="permission.INTERNET" />
But even after this,the Android web view webpage does not load, I cannot find the error. Help me find it. Do I need to add something else, or is there an error in the existing files that I have given
try these codes instead.
class:
public class WebViewClientDemoActivity extends Activity {
private WebView webView;
private String url = "https://www.google.com";
private ProgressBar progressBar;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setWebViewClient(new myWebClient());
webView.loadUrl(url);
}
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 onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
progressBar.setVisibility(View.GONE);
}
}, 10000);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:indeterminate="false"/>
</RelativeLayout>

I can't load a specific URL using WebView

I can't load this specific url (http://sia.bogota.unal.edu.co/academia/) using WebView, but I can load other URLs. (See images below)
This is my code
Activity Class:
package co.luisfer.webview;
import android.app.Activity;
import android.app.ProgressDialog;
import android.net.http.SslError;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
Activity activity;
private ProgressDialog progDailog;
private WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity = this;
progDailog = ProgressDialog.show(activity, "Loading", "Please wait...", true);
progDailog.setCancelable(false);
webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new MyWebViewClient());
WebSettings settings = webView.getSettings();
settings.setBuiltInZoomControls(false);
settings.setSupportZoom(false);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setAllowFileAccess(true);
settings.setDomStorageEnabled(true);
webView.loadUrl("http://sia.bogota.unal.edu.co/academia");
}
private class MyWebViewClient extends WebViewClient {
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progDailog.show();
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
progDailog.dismiss();
}
}
}
XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/webview"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
I don't have any clue what's going on.
Can you help me?
Thanks in advance
PS: I have already set up the internet permission:
<uses-permission android:name="android.permission.INTERNET" />

How to load external webpage in WebView

My problem is that the webpage is not loaded inside the WebView.
mWebview.loadUrl("http://www.google.com"); launches the web browser...
This is the code of my activity:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class Main extends Activity {
private WebView mWebview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.loadUrl("http://www.google.com");
setContentView(mWebview);
}
}
I added the required permission in the Manifest:
<uses-permission android:name="android.permission.INTERNET" />
Thanks to this post, I finally found the solution. Here is the code:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import android.annotation.TargetApi;
public class Main extends Activity {
private WebView mWebview ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
#SuppressWarnings("deprecation")
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
#TargetApi(android.os.Build.VERSION_CODES.M)
#Override
public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
// Redirect to deprecated method, so you can use it in all SDK versions
onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
}
});
mWebview .loadUrl("http://www.google.com");
setContentView(mWebview );
}
}
try this
webviewlayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/help_webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
/>
In your Activity:
WebView webView;
setContentView(R.layout.webviewlayout);
webView = (WebView)findViewById(R.id.help_webview);
webView.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.google.com");
Update
Add webView.setWebViewClient(new WebViewController()); to your Activity.
WebViewController class:
public class WebViewController extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
public class WebViewController extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
webView.setWebViewClient(new WebViewController());
Please use this code:-
Main.Xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#drawable/background">
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#drawable/top_heading"
android:id="#+id/rlayout1">
<TextView android:layout_width="wrap_content"
android:layout_centerVertical="true" android:layout_centerHorizontal="true"
android:textColor="#ffffff" android:textSize="22dip"
android:textStyle="bold" android:layout_height="wrap_content"
android:text="More Information" android:id="#+id/txtviewfbdisplaytitle" />
</RelativeLayout>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_below="#+id/rlayout1"
android:id="#+id/rlayout2">
<WebView android:id="#+id/webview1" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0" />
</RelativeLayout>
</RelativeLayout>
MainActivity.Java
public class MainActivity extends Activity {
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Button btnBack;
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview=(WebView)findViewById(R.id.webview1);
webview.setWebViewClient(new MyWebViewClient());
openURL();
}
/** Opens the URL in a browser */
private void openURL() {
webview.loadUrl("http://www.google.com");
webview.requestFocus();
}
}
Try this code if any query ask me.
It's very simple try integrate these lines of code
first take permission in the Android Manifest file
<uses-permission android:name="android.permission.INTERNET" />
then write some code in you Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.MainActivity">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/help_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Then write these code in your MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity{
private WebView mWebview ;
String link = "";// global variable
Resources res;// global variable
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_modernherbal_main);
mWebview = (WebView) findViewById(R.id.help_webview);
WebSettings webSettings = mWebview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("http://www.example.com");
}
}
Try this it'll help you to solve your problem
just go into XML file and give id to your webView then in java paste these line:
public class Main extends Activity {
private WebView mWebview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Your_layout_file_name);
mWebview = (WebView)findViewById(R.id.id_you_gave _to_your_wenview_in_xml);
mWebview.loadUrl("http://www.google.com");
}
}
I used this code that was cool. but have an error. " neterr_cleartext_not_permitted"
show when you use this code then you will face this problem..
I got a solution of this.you have to add this in your AndroidManifest.xml near about Application
android:usesCleartextTraffic="true"
<uses-permission android:name="android.permission.INTERNET" /> // ignore if you already added. outside of Application.
You can do like this.
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("Your URL goes here");
try this;
webView.loadData("<iframe src='http://www.google.com' style='border: 0; width: 100%; height: 100%'></iframe>", "text/html; charset=utf-8", "UTF-8");
Add Internet permission in AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
In your Layout:
<?xml version="1.0" encoding="utf-8"?>
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webView"
/>
In your Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
progressDialog = new ProgressDialog(this);
url_Api = "https://learn.microsoft.com/en-us/learn";
webView = this.findViewById(R.id.webView);
progressDialog.setMessage(getString(R.string.connection_Wait));
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(true);
progressDialog.show();
LoadUrlWebView( url_Api );
}catch (Exception e){
Log.w(TAG, "onCreate", e);
}
}
private void LoadUrlWebView( String url_api ) {
try {
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new MyWebChromeClient( url_api ));
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
webView.loadUrl(url_api);
} catch (Exception e) {
Log.w(TAG, "setUpNavigationView", e);
}
}
private class MyWebChromeClient extends WebChromeClient {
private String urlAccount;
public MyWebChromeClient( String urlAccount ) {
this.urlAccount = urlAccount;
}
#Override
public void onProgressChanged(WebView view, int newProgress) {
try {
//Tools.LogCat(context, "INSIDE MyWebChromeClient | onProgressChanged / newProgress1:" + newProgress);
progressDialog.setMessage(newProgress + "% " + getString(R.string.connection_Wait));
if (newProgress < 100 && !progressDialog.isShowing()) {
if (progressDialog != null)
progressDialog.show();
}
if (newProgress == 100) {
if (progressDialog != null)
progressDialog.dismiss();
}
}catch (Exception e){
Log.w( "onProgressChanged", e);
}
}
#Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
sharedPreferences = new Shared_Preferences( context );
sharedPreferences.setPageWebView(view.getUrl());
}
}
Add WebView Client
mWebView.setWebViewClient(new WebViewClient());
You need to add WebView client
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
}
});
also you can use onPageFinished to do task after webview done loading web page
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:id="#+id/webView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
webView .loadUrl("http://www.google.com");
webView.setWebViewClient(new MyWebViewClient());
}
}
AndroidManifest.xml: (add uses-permission and android:usesCleartextTraffic)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.MyApplication">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Simple Webview activity in kotlin :
class WebViewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
intent.getStringExtra(URL_EXTRA)?.let { url ->
WebView(this).apply {
settings.javaScriptEnabled = true // check if your need this
settings.domStorageEnabled = true
settings.setSupportZoom(true)
settings.builtInZoomControls = true
settings.displayZoomControls = false
loadUrl(url)
setContentView(this)
}
}
}
companion object {
const val URL_EXTRA = "url"
}
}
Add below method in your activity class.Here browser is nothing but your webview object.
Now you can view web contain page wise easily.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && browser.canGoBack()) {
browser.goBack();
return true;
}
return false;
}

Categories

Resources