I have problem with web view.
I don't know how to handle loading data( timeout) in web client when I open web success i turn off wifi and continues open web client. it loading forever.(
Here is xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/web_assignment"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include
android:id="#+id/retry_assignment"
layout="#layout/layout_retry_web"
android:visibility="gone"
/>
<com.lusfold.spinnerloading.SpinnerLoading
android:id="#+id/spinnerLoalding_assignment"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<WebView
android:id="#+id/layout_assignment"
android:layout_width="match_parent"
android:layout_height="match_parent"></WebView>
</RelativeLayout>
Code is:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//
tokenManager = new TokenManager(getApplicationContext());
token = tokenManager.GetTokenID();
if (networkStateReceiver.isConnected(this))
checkTimeToken(token);
else {
setVisibleLayout();
}
}
#Override
protected void onStart() {
super.onStart();
}
#Override
protected void onResume() {
super.onResume();
}
#Override
protected void onPause() {
super.onPause();
}
#Override
protected void onStop() {
super.onStop();
}
private void setVisibleLayout() {
webView.setVisibility(View.GONE);
relativeLayout.setVisibility(View.VISIBLE);
spinnerLoading.setVisibility(View.GONE);
btnRetry.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("onclick", "Tap reload");
loadweb();
}
});
}
private void checkTimeToken(final String token) {
//
}
private void loadweb() {
final String strUrl = URL_Utils.Assignments_Load + token;
Log.d("URL_asiagnmentss :", strUrl);
if (networkStateReceiver.isConnectedNetwork(this)) {
webView.loadUrl(strUrl);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
spinnerLoading.setVisibility(View.VISIBLE);
webView.setVisibility(View.INVISIBLE);
spinnerLoading.setPaintMode(1);
spinnerLoading.setCircleRadius(20);
spinnerLoading.setItemCount(8);
Log.d("onPageStarted", "onPageStarted");
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
spinnerLoading.setVisibility(View.INVISIBLE);
webView.setVisibility(View.VISIBLE);
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
}
});
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setSupportZoom(true);
webView.setVisibility(View.VISIBLE);
relativeLayout.setVisibility(View.GONE);
} else
setVisibleLayout();
}
Firts of all create one Boolean variable default true which track of whether timeout or not.
After that on your WebViewClient's onPageStarted method you have to put one thread which trigger at your given time.
In the last, onPageFinished set variable false
As shown in the code :-
public class MyWebViewClient extends WebViewClient {
boolean timeout = true;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Runnable run = new Runnable() {
public void run() {
if(timeout) {
// do what you want
showAlert("Connection Timed out", "Whoops! Something went wrong. Please try again later.");
}
}
};
myHandler.postDelayed(run, 5000);
}
public void onPageFinished(WebView view, String url) {
timeout = false;
}
}
Related
As soon as the Activity started from intent, need to display a ProgressBar. Once WebView is displayed, then ProgressBar need to be hidden.But not able to show ProgressBar in screen. Below is my 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">
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:layout_centerInParent="true"/>
<WebView
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:visibility="invisible"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
below is my Activity,
public class WebViewActivity extends Activity {
String TAG= WebViewActivity.class.getSimpleName();
String url=null;
WebView webView;
ProgressBar progressBar=null;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
//getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
setContentView(R.layout.activity_webview);
progressBar=findViewById(R.id.progress_bar);
Intent intent=getIntent();
this.url=intent.getStringExtra("url");
webView =(WebView)findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
webView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.INVISIBLE);
AndroidLogger.log(5,TAG," error response"+error);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
AndroidLogger.log(5,TAG,"started"+url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.INVISIBLE);
}
});
}
}
After starting Intent for WebViewActivity, instead of showing ProgressBar getting a blank white screen, until WebView opens up. So how to solve this. Anybody help with this.
Your xml file seems correct BUT your java file having unnecessary code stuff. just adding progress bar in xml will not work. you need to hide/show inside your java file.
correct it step by step and follow this.
public class WebViewActivity extends Activity implements Listener {
String TAG= PaymentGatewayActivity.class.getSimpleName();
String url=null;
String deviceType="ANDROID", name, password, code, pId=null;
String cur=null;
WebView webView;
ProgressBar progressBar=null;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_webview);
progressBar=(ProgressBar) findViewById(R.id.payment_progress_bar);
progressBar.setVisibility(View.VISIBLE);
AndroidLogger.log(5,TAG,"progress bar made visible");
Intent intent=getIntent();
this.pId=intent.getStringExtra("pId");
// HERE CALLING RETROFIT
CommunicationManager communicationManager = new CommunicationManager(WebViewActivity.this, this,pId );
communicationManager.getLink("get url");
}
//THIS METHOD WILL INVOKED WHEN GETTING SUCCESS RESPONSE
#Override
public void getUrlSuccessResponse(String url, String pId, String cur) {
this.url = url;
this.pId = pId;
this.cur = cur;
SharedPreferences sharedpreferences = this.getSharedPreferences(this.getResources().getString(R.string.app_preferences), Context.MODE_PRIVATE);
this.name = sharedpreferences.getString(this.getResources().getString(R.string.name), "");
this.password = sharedpreferences.getString(this.getResources().getString(R.string.password), "");
this.code = sharedpreferences.getString(this.getResources().getString(R.string.code), "");
webView = (WebView) findViewById(R.id.webView);
webView.setVisibility(View.VISIBLE);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
webView.setBackgroundColor(Color.TRANSPARENT) //set this one to remove unnecessary background
//setup custom webview client here
webView.setWebViewClient(new PayWeb());
//load url inside webview
try {
String postData = "type=" + URLEncoder.encode("ANDROID", "UTF-8") + "&password=" + URLEncoder.encode(password, "UTF-8") + "&code=" + URLEncoder.encode(code, "UTF-8") + "&cur=" + URLEncoder.encode(cur, "UTF-8") + "&name=" + URLEncoder.encode(subscriberId, "UTF-8") + "&pId=" + URLEncoder.encode(pId, "UTF-8");
webView.postUrl(url, postData.getBytes());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public class PayWeb extends WebViewClient{
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progressBar.setVisibility(View.VISIBLE);
return false; // then it is not handled by default action
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
progressBar.setVisibility(View.INVISIBLE);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
//show progressbar
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//hide progressbar
progressBar.setVisibility(View.INVISIBLE);
}
}
#Override
public void getUrlFailureResponse(int statusCode, String status) {
//manage webpage faliour case
}
#Override
public void getUrlNetworkFailureException(int statusCode, String status) {
//manage network faliour case
}
}
I'm building an Webview app which displays my website. My website contains clickable mobile number, I need to open dialer when user clicks it.
I've gone through this question.
Since I'm new to Android development I don't know exactly where to paste that code.
Here Is my Mainactivity.java code
public class MainActivity extends AppCompatActivity {
private WebView webView;
private ProgressBar mProgressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView= findViewById(R.id.web);
mProgressBar= findViewById(R.id.progressbar);
mProgressBar.setMax(100);
webView.loadUrl("https://");
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient(){
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
mProgressBar.setProgress(newProgress);
}
#Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
}
#Override
public void onReceivedIcon(WebView view, Bitmap icon) {
super.onReceivedIcon(view, icon);
}
});
}
#Override
public void onBackPressed(){
if (webView.canGoBack()) {
webView.goBack();
}else {
finish();
}
}}
You need to Override the shouldOverrideUrlLoading() method in setWebViewClient()
SAMPLE CODE
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("tel:"))
{
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}else {
progressBar.setVisibility(view.VISIBLE);
view.loadUrl(url);
return true;
}
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
}
});
I am implementing a WebView to load a web page for an app. I have set up everything correctly but the issue i have is that the side bar for the website in the webViewis not coming up. The icon shows quite alright and also responds to clicks but it doesn't come out or expand. I tried loading the website in a Chrome mobile browser and it worked well. What could be wrong with my code? Please someone help me out.
MainActivity.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:layout_alignParentTop="true"
android:id="#+id/pull_to_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/my_web_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</WebView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
private ProgressDialog mProgressDialog;
private SwipeRefreshLayout swipeRefreshLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = findViewById(R.id.my_web_view);
loadWebView();
//implementing pull to refresh
swipeRefreshLayout = findViewById(R.id.pull_to_refresh);
swipeRefreshLayout.setRefreshing(true);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
loadWebView();
}
});
}
private void loadWebView() {
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient(){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setMessage("Loading");
if (!swipeRefreshLayout.isRefreshing()){
mProgressDialog.show();
}
mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
showToast();
}
});
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (mProgressDialog != null) {
mProgressDialog.dismiss();
}
swipeRefreshLayout.setRefreshing(false);
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
showToast();
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return super.shouldOverrideUrlLoading(view, request);
}
});
myWebView.loadUrl("https://app.gerocare.org/doctor");
}
private void showToast() {
Toast.makeText(this, "connection error", Toast.LENGTH_SHORT).show();
}
}
I have set orientation MainWebActivity to PORTRAIT and GameActivityLandscape to LANDSCAPE in AndroidManifest.xml when i start activity GameActivityLandscape in shouldOverrideUrlLoading ,but when i press back button from GameActivityLandscape then there appears a blank page,I want MainWebActivity as like first time load
My MainWebActivity is
public class MainWebActivity extends Activity {
private WebView myWebView;
ProgressBar progressBar;
boolean internet;
Bundle webState=null;
String GMTag="MainWebActivity ";
static String lodUrl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
myWebView = (WebView) findViewById(webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new myWebClient());
myWebView.setWebChromeClient(new WebChromeClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
//myWebView.clearCache(true);
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
if (savedInstanceState == null) {
Log.d(GMTag, " onResume webview load ");
myWebView.loadUrl("http://html5games.com/Game/Civilizations-Wars-Master-Edition/2001681a-0f53-4691-9318-04e419ac7c0c");
}
}
public class myWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
Log.d(GMTag, "Main Web shouldOverrideUrlLoading test" + url.toString());
return handleUri(url);
}
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Log.d(GMTag, "Main Web android N shouldOverrideUrlLoading " + request.getUrl().toString());
//final Uri uri = request.getUrl();
return handleUri(request.getUrl().toString());
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
/*public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){
//Your code to do
Toast.makeText(MainWebActivity.this, "Your Internet Connection May not be active Or " + error.getDescription() , Toast.LENGTH_LONG).show();
}*/
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
if(errorCode==404){
Toast.makeText(MainWebActivity.this, "File or directry not found" , Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(MainWebActivity.this, "Your Internet Connection May not be active Or " + description, Toast.LENGTH_LONG).show();
}
}
}
private boolean handleUri(String url) {
if (url.contains("fg_domain=play.famobi.com&fg_aid=A1000-1&fg_uid=2001681a-0f53-4691-9318-04e419ac7c0c&fg_pid=4638e320-4444-4514-81c4-d80a8c662371&"))
{
Intent myIntent = new Intent(MainWebActivity.this, GameActivityLandscape.class);
myIntent.putExtra("URL", url); //Optional parameters
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
MainWebActivity.this.startActivity(myIntent);
progressBar.setVisibility(View.GONE);
} else {
myWebView.loadUrl(url);
}
return true;
}
#Override
protected void onPause() {
super.onPause();
myWebView.onPause();
Log.d(GMTag, " Web onPause called " + webState);
}
#SuppressLint("SetJavaScriptEnabled")
#JavascriptInterface
#Override
protected void onResume() {
super.onResume();
myWebView.onResume();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
Log.d(GMTag, " onSaveInstanceState ");
super.onSaveInstanceState(outState);
webState=outState;
myWebView.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
Log.d(GMTag, " webview restored in onRestoreInstanceState ");
super.onRestoreInstanceState(savedInstanceState);
myWebView.restoreState(savedInstanceState);
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
This is my GameActivityLandscape activity
public class GameActivityLandscape extends Activity {
private WebView myWebView;
static String gameRotation, gameUrl;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Intent intent = getIntent();
gameRotation = intent.getStringExtra("ORIENTATION");
gameUrl = intent.getStringExtra("URL");
//progressBar=(ProgressBar)findViewById(R.id.game_progressBar);
myWebView = (WebView) findViewById(R.id.game_webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new myWebClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
if (savedInstanceState == null) {
myWebView.loadUrl(gameUrl);
}
}
public class myWebClient extends WebViewClient {
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Log.d("Game", "onError landscape called " + failingUrl);
}
}
#Override
protected void onResume() {
super.onResume();
myWebView.onResume();
Log.d("Game", "onresume called of gameacticity landcape ");
}
#Override
protected void onPause() {
super.onPause();
Log.d("Game", "onPause called of gameacticity landcape ");
myWebView.onPause();
}
#Override
public void onBackPressed() {
super.onBackPressed();
Log.i("Gaming mania", "Play game landscape activity finish");
finish();
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
I am using webview with ProgressDialog. My issue is that on the Initial Launch, the progressDialog appears abd load properly but whenever I click a link (post links) inside webview, the progress is not showing. Here's the code -
public class Xyz extends WebViewClient
{
public void onLoadResource (WebView view , String url ) {
if (progressDialog == null ) {
progressDialog = new ProgressDialog(getActivity());
progressDialog . setMessage( "Loading..." );
progressDialog.setIndeterminate(true);
progressDialog . show();
}
}
public void onPageFinished(WebView view , String url ) {
if (progressDialog . isShowing()) {
progressDialog . dismiss();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
You are overriding the wrong method to achieve your goal. I've put up an Activity here which works as intended.
I'm using onPageStarted() instead of onLoadResource().
Activity MainActivity.java:
public class MainActivity extends AppCompatActivity {
ProgressDialog progressDialog;
WebViewClient mClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mClient = new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if (progressDialog == null) {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.setIndeterminate(true);
}
if(!progressDialog.isShowing()) {
progressDialog.show();
}
}
public void onLoadResource(WebView view, String url) {
}
public void onPageFinished(WebView view, String url) {
if (progressDialog!=null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
};
WebView webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(mClient);
webView.loadUrl("http://www.google.com");
}
}
Layout activity_main.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: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.prasad.myapplication.MainActivity">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
Try to track the webview load progress using webChoromeClient like this:
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
if (progress == 100) {
progressBar.setVisibility(View.GONE);
} else {
progressBar.setVisibility(View.VISIBLE);
}
}
});
and replace your webview client as follwing
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}