I am creating a simple app Android webview, but also following the guides, I can not implement a progress bar to my code.
how can I implement a loading bar that will disappear after the page loads?
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView view = (WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new MyBrowser());
view.loadUrl("http://www.abcdefcsadfg.org");
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
You can initialize a Progressbar in onCreate, like below and set its initial visibility to View.GONE.
progress = (ProgressBar) findViewById(R.id.progressBar);
progress.setVisibility(View.GONE);
And your MyBrowser should look like this
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
progress.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progress.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
}
UPDATE:
Check this link
You should declare progress bar in xml file and refer to it in the main activity so that it appears in your application.
Set its progress in MyBrowser.
Refer this link, it will help you.
http://javatechig.com/android/progressbar-while-loading-webview
Related
I am using tawk.to to insert an online chat in my app, with a simple WebView to load the chat, but when I put the url of tawk.to it does not display something. With any other url it works fine and loads the page, but not the chat. I also tried the page 'tawk.to' and it loads.
public class ChatOnlineActivity extends AppCompatActivity {
WebView chatOnlineWebVIew;
ProgressBar loadingChat;
/*MORE CODE HERE*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_online);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
String url="https://tawk.to/chat/597771645dfc8255d623ede4/default?fbclid=IwAR3QB_DOoEu9ePTSPJbayOcTIaWtXjuJMUn46qLFTRVNnpPZBTZhsOy6lrs";
//String url="https://tawk.to/"; //JUST TEST URLS
loadingChat=(ProgressBar)findViewById(R.id.loadingChatOnline);
chatOnlineWebVIew = (WebView) findViewById(R.id.chatWebView);
WebSettings chatSetting=chatOnlineWebVIew.getSettings();
chatSetting.setJavaScriptEnabled(true);
//chatSetting.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
chatOnlineWebVIew.setWebViewClient(new chatWebClient());
//chatOnlineWebVIew.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
chatOnlineWebVIew.loadUrl(url);
/*MORE CODE HERE*/
private class chatWebClient extends WebViewClient{
#Override
public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
loadingChat.setVisibility(View.VISIBLE);
return super.shouldOverrideKeyEvent(view, event);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}
#Override
public void onPageFinished(WebView view, String url) {
loadingChat.setVisibility(View.GONE);
chatOnlineWebVIew.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(),url,Toast.LENGTH_LONG).show(); //just massage to test if the urls is correct
super.onPageFinished(view, url);
}
}
}
I was also facing the same issue, my WebView was loading the url but not displaying but now i have fixed my issue by adding these following lines of code
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setSupportZoom(true);
webSettings.setDefaultTextEncodingName("utf-8");
and my final activity is look like below
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import org.jetbrains.annotations.NotNull;
public class TawkToChatWebView extends AppCompatActivity {
private WebView webView;
Activity activity ;
private ProgressDialog progDailog;
#SuppressLint({"NewApi", "SetJavaScriptEnabled"})
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tawktochat);
if (getSupportActionBar()!=null)
{
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
webView=findViewById(R.id.wv_chat);
activity = this;
progDailog = ProgressDialog.show(activity, "Loading","Please wait...", true);
progDailog.setCancelable(false);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setSupportZoom(true);
webSettings.setDefaultTextEncodingName("utf-8");
webView.setWebViewClient(new WebViewClient(){
#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();
Log.e("loading","finished");
}
});
webView.loadUrl("ADD_YOUR_DIRECT_CHAT_LINK_HERE");
}
#Override
public boolean onOptionsItemSelected(#NonNull #NotNull MenuItem item) {
if (item.getItemId()==android.R.id.home)
{
finish();
}
return super.onOptionsItemSelected(item);
}
}
If I use a button it works, but it redirects to a default browser.
I want to use the webview because i want to display the website in my webview and avoid the address bar to display.
Here is my code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
WebView webview = (WebView) findViewById(R.id.webView1);
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=http://178.239.16.28/fzs/sites/default/files/dokumenti-vijesti/sample.pdf");
webview.setWebViewClient (new WebViewClient());
}
Try below code sequence and let me know if any issues. Don't forget to add permission to your AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
Change to below :
WebView webview = (WebView) findViewById(R.id.webView1);
webview.setWebViewClient (new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=http://178.239.16.28/fzs/sites/default/files/dokumenti-vijesti/sample.pdf");
And use below snippts
private class HelloWebViewClient extends WebViewClient {
#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);
}
Your url is redirecting.
You'll have to override shouldOverrideUrlLoading
Give the host application a chance to handle the key event synchronously. e.g. menu shortcut key events need to be filtered this
way. If return true, WebView will not handle the key event. If return
false, WebView will always handle the key event, so none of the super
in the view chain will see the key event. The default behavior returns
false.
Parameters
view The WebView that is initiating the callback.
event The
key event.
Returns True if the host application wants to handle the
key event itself, otherwise return false
Use this lines ..
package org.example.webviewdemo;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewDemo extends Activity {
private WebView webView;
Activity activity ;
private ProgressDialog progDailog;
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
activity = this;
progDailog = ProgressDialog.show(activity, "Loading","Please wait...", true);
progDailog.setCancelable(false);
webView = (WebView) findViewById(R.id.webview_compontent);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setWebViewClient(new WebViewClient(){
#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();
}
});
webView.loadUrl("http://docs.google.com/gview?embedded=true&url=http://178.239.16.28/fzs/sites/default/files/dokumenti-vijesti/sample.pdf");
}
}
It will show loader until loading the file. then loader get hide once file have loaded using google docs. hope this will help you.
I was wondering if there exits another effective way to get the URL that will load up on the WebView. My current code gives the current Url and not the one that will be loaded.
For example if my WebView load this: http://stackoverflow.com
After loading If I click on Questions, I would not get this url http://stackoverflow.com/questions, for some reason I would get http://stackoverflow.com
So my question is how would you get the url that will be loading on a WebView?
This is my code, please help!
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity {
// Declaring
WebView browser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing
browser = (WebView) findViewById(R.id.webView1);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setLoadWithOverviewMode(true);
browser.getSettings().setUseWideViewPort(true);
browser.getSettings().setBuiltInZoomControls(true);
// Loading
browser.loadUrl("http://stackoverflow.com");
browser.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onLoadResource(WebView view, String url) {
//here I get the Url, but its not accurate. Sometimes it works, sometiems it doesn't
Toast.makeText(getApplicationContext(), browser.getUrl(),
Toast.LENGTH_SHORT).show();
}
});
}
}
Try to get url from this function :
EDIT:
browser.loadUrl("http://stackoverflow.com");
browser.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Toast.makeText(getApplicationContext(), url, Toast.LENGTH_SHORT).show();
Log.v("TEST", url);
if(url.equals("http://stackoverflow.com/questions")){
Toast.makeText(getApplicationContext(), "SKIP", Toast.LENGTH_SHORT).show();
}
else{
view.loadUrl(url);
}
return true;
}
});
Reference :
shouldOverrideUrlLoading
I know its very late but,for other users if it helps it would be my pleasure.
In ur WebViewClient u can use :
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String webUrl=view.getUrl();
return true;
}
}
I'm trying to implement a loading dialog in my webview application but whatever I do it just won't work.
What I want is the following, everytime someone clicks a link in the webview the loading dialog has to popup like this:
(source: wordpress.com)
And when the loading is done it has to disappear. Can anyone show me how to do it in my own source? I've tried a lot of things but there's always an issue so I really don't know how to.
This is my WebviewActivity:
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebviewActivity extends MainActivity {
private WebView myWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
myWebView = (WebView)findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient());
myWebView.requestFocus(View.FOCUS_DOWN);
myWebView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus()) {
v.requestFocus();
}
break;
}
return false;
}
});
}
#Override
public void onResume() {
super.onResume();
if ( isOnline() == true )
myWebView.loadUrl(webLink);
else if ( isOnline() == false )
showNoConnectionDialog();
}
#Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
}
Thank you very much for your help!!
You have to override the WebViewClient like this,
webview.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, final String url) {
progressDialog.cancel();
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressDialog.show();
}
});
webview.loadUrl(TwitterUrl);
This is my final solution:
myWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progressDialog.show();
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
progressDialog.dismiss();
}
});
This can be possible with WebChromeClient. It does worked for me.
Code snippet :
progressDialog.show(); // Add before page load
webView.setWebChromeClient(new HelpWebChromeClient());
private class HelpWebChromeClient extends WebChromeClient {
#Override
public void onProgressChanged(WebView view, int newProgress) {
if(newProgress == 100)
progressDialog.dismiss();
}
}
I've tried everything I know how to do to kill it. What I want it to do is load the webpage and then kill the ProgressDialog. How do?
package com.calebfultz.package;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
public class Lists extends Activity {
private WebView webView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
ProgressDialog pd = ProgressDialog.show(Lists.this, "",
"Loading. Please wait...", true);
// Create reference to UI elements
webView = (WebView) findViewById(R.id.web_engine);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://cfultz.tumblr.com");
// workaround so that the default browser doesn't take over
webView.setWebViewClient(new MyWebViewClient()
);
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
You may want to make your ProgressDialog variable as a member variable of the Lists class and in your shouldOverrideUrlLoading method, add the line
pd.dismiss();
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// Show your progress dialog here
prDialog = ProgressDialog.show(this, "", "Loading, please wait...", true);
super.onPageStarted(view, url, favicon);
}
public void onPageFinished(WebView view, String url) {
//Finish your progress dialog here
if (prDialog.isShowing() || prDialog != null) {
prDialog.dismiss();
}
super.onPageFinished(view, url);
}
I think you can override the onPageFinished function in MyWebViewClient: you can dismiss the ProgressDialog in this function. Or you can override the onProgressChanged in MyWebChromeClient(extends WebChromeClient): when the progress is 100, you can dismiss the ProgressDialog.
Please see this code:
// the init state of progress dialog
mProgress = ProgressDialog.show(this, "Loading", "Please wait for a moment...");
// add a WebViewClient for WebView, which actually handles loading data from web
mWebView.setWebViewClient(new WebViewClient() {
// load url
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
// when finish loading page
public void onPageFinished(WebView view, String url) {
if(mProgress.isShowing()) {
mProgress.dismiss();
}
}
});
it is working fine for me.........enjoy.
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
//start loading animation
//loading is a ProgressBar type
this.loading.setVisibility(0);
view.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if(progress >= 100) {
this.loading.setVisibility(8);
}
}
});