I'm working on an Android App that simply wraps a mobile version of a website inside of a native App container.
At present I call (example):
wv.loadUrl("http://www.websitehere.com/");
I have exported the entire project and tested it on my phone and when loading a new page inside of the App it takes a few moments, however not being on a browser there is no loading indicator, so it is a still screen on the current page for a few moments (depending on internet speed) while the new page loads.
What would be the best way to implement a loading indicator so that the user knows that a new page is infact loading?
EDIT: Loading added, but only for the initial pageload of app.
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ProgressDialog pd = ProgressDialog.show(this, "Page is", "Loading...",true);
WebView wv = (WebView)findViewById(R.id.my_webview);
wv.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if(pd.isShowing()&&pd!=null);
{
pd.dismiss();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
pd = ProgressDialog.show(this, "Page is", "Loading...", true);
view.loadUrl(url);
return true;
}
});
wv.loadUrl("http://www.websitehere.com");
}
}
If your website is html based and not server side (asp/php etc) you could package the website in the app so no internet is required. Downside to this is updating 2 websites.
Alternatively you could add a loading script (javascript) to the website itself.
You could start a ProgressDialog before you load the website and dismiss it after the loading is done.
Code should look something like this:
ProgressDialog progressDialog = ProgressDialog.show(this, "", "Loading...");
wv.loadUrl("http://www.yourwebsite.com");
progressDialog.dismiss();
EDIT: new code for every new url clicked
You should add this to your WebViewClient and also make you (ProgressDialog) pd a member variable.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
pd = ProgressDialog.show(this, "Page is:", "Loading...",true);
view.loadUrl(url);
return true;
}
EDIT:
You should make the ProgressDialog pd a member variable.
public class Main extends Activity {
private ProgressDialog pd;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pd = ProgressDialog.show(this, "Page is", "Loading...",true);
WebView wv = (WebView)findViewById(R.id.my_webview);
wv.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if(pd.isShowing()&&pd!=null);
{
pd.dismiss();
}
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
pd = ProgressDialog.show(Main.this, "Page is", "Loading...", true);
}
});
wv.loadUrl("http://www.websitehere.com");
}
}
EDIT: using onPageStarted.
Related
I have a WebView in my Android app which displays a web page with a specific URL. The URL is a signal to the app to close the WebView and perform other actions.
I'd like to display a ProgressDialog over the WebView while the app loads the functions required by the URL. However when the ProgressDialog appears, it appears dimmed, as if there is an overlay superimposed on top of it. Displaying an AlertDialog appears as normal. Can anyone advise why the PD appears dimmed?
private class WebView extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, final String url) {
if (url.contains("my/special/url")) {
// ProgressDialog message appears dimmed
ProgressDialog progress;
progress = new ProgressDialog(view.getContext());
progress.setMessage("Loading...");
progress.show();
progress.setCancelable(false);
progress.setCanceledOnTouchOutside(false);
// AlertDialogs appear as normal
AlertDialog.Builder builder = new AlertDialog.Builder(context);
AlertDialog dialog;
builder.setMessage("Loading");
}
return true;
}
}
In your code your private Inner class name is WebViewthat need to change.
It could be MyWebView or anything else. But should not WebView.
Because,
#Override
public boolean shouldOverrideUrlLoading(WebView view, final String url) {
}
In this override method there is a paramiter WebView that is build in class in android from android.webkit package.
If you keep your class name WebView that means you not overriding method form WebViewClient shouldOverrideUrlLoadingand it will not work.
Then
You can override onPageStarted to show your progress dialog
because it call on loading the page....
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
if (url.contains("my/special/url")) {
// ProgressDialog message appears dimmed
ProgressDialog progress;
progress = new ProgressDialog(view.getContext());
progress.setMessage("Loading...");
progress.show();
progress.setCancelable(false);
progress.setCanceledOnTouchOutside(false);
// AlertDialogs appear as normal
AlertDialog.Builder builder = new AlertDialog.Builder(context);
AlertDialog dialog;
builder.setMessage("Loading..");
}
}
You must ensure that if (url.contains("my/special/url")) is true. otherwise it never execute...because if(false) never execute ... in this case else part will execute if have..
For showing progress in webview, WebViewChromeClient is used. Try to implement webview using below example. Hope it solves your problem.
public class webView extends AppCompatActivity {
private String mUrl;
public WebView mWebView;
// public ProgressBar progressBar;
private ProgressBar progressBar;//
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
Log.e("Webview class", "I'm in web view");
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
//set progress bar max limit
progressBar.setMax(100);
//get the url from the intent of this activity called from the fragments
mUrl = getIntent().getExtras().getString("url");
// set the title of webview to be th url
setTitle(mUrl);
mWebView = (WebView) findViewById(R.id.webview);
//mWebView = new WebView(this);
mWebView.setWebViewClient(new myWebViewClient());
mWebView.setWebChromeClient(new WebChromeClientDemo());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(mUrl);
//setContentView(mWebView);
}
private class myWebViewClient 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);
progressBar.setVisibility(View.GONE);
progressBar.setProgress(100);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
}
}
private class WebChromeClientDemo extends WebChromeClient {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
}
}
How do you create a ProgressDialog without showing it? When the WebView starts trying to open a page I want a progress dialog to appear if the page hasn't loaded after two seconds (and disappear if it still hasn't loaded after 30 seconds). The following code is failing because ProgressDialog progress = new ProgressDialog(MainActivity.this);, despite what I've seen in various SO answers, throws a NullPointerException error.
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
// ProgressDialog progress = new ProgressDialog(MainActivity.this); // gives NullPointerException
ProgressDialog progress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
webView.setWebViewClient(new WebViewClient() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap facIcon) {
timerBeginDialog(2000, progress);
progress.setCancelable(true);
timerRemoveDialog(30000, progress);
}
#Override
public void onPageFinished(WebView view, String url) {
drawer.closeDrawer(GravityCompat.START);
progress.dismiss();
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
view.loadUrl("file:///android_asset/www/error.html");
drawer.openDrawer(GravityCompat.START);
progress.dismiss();
}
});
.
.
}
public void timerBeginDialog(long time, final ProgressDialog pd){
new Handler().postDelayed(new Runnable() {
public void run() {
pd = ProgressDialog.show(MainActivity.this, "", "Loading...", true); // gives error "Cannot assign a value to final variable pd"
}
}, time);
}
public void timerRemoveDialog(long time, final ProgressDialog pd){
new Handler().postDelayed(new Runnable() {
public void run() {
pd.dismiss();
}
}, time);
}
That's because the context has not been created yet when your initializer runs.
Just place the initialization inside onCreate method.
Move
ProgressDialog progress = new ProgressDialog(MainActivity.this);
inside onCreate
You cannot pass a variable by reference on Java, so what you need to do is to define the variable within the class, which you apparantly have done, and its name is progress, but withing the method call you're referring to variable 'pd' which is the one it takes as parameter.
You should instead set the value of 'progress' to the new ProgressDialog like this:
progress = ProgressDialog.show(MainActivity.this, "", "Loading...", true);
and then on other method
progress.dismiss();
It seems that I made some inaccuracies or I missed something regarding public class myWebViewClient class and public void onPageFinished(WebView view, String url). Initially and in the beginning I tried to launch www.yahoo.com and after it finishes loading I fire a toast from inside the public void onPageFinished(WebView view, String url) as indication to me the loading of the www.yahoo.com is finished, and up to here every thing is fine. Inside my RelativeLayout I have a button and edittext and my webview is layedout-above them. The purpose of having an edittext is to enable the user to enter their webite and by clicking on go-button, the entered website should be launched and I expect to see the toast I have inside public void onPageFinished(WebView view, String url) is fired again after the new website finishes loading. But what happens is, once the user enters a new website in the edittext field and immediately after they press go_button the toast fires before even the website is loaded and it fires once again after the same web site is finished loading. Actually, I expect the toast to be fired only after every website finishes loading, but what is happening with me is not like that. Please provide me with suggestions to prevent occurring such problem.
JavaCode:
OnClickListener btnGoListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
webView00.loadUrl(etUrl.getText().toString());
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_web_page00);
webView00 = (WebView) findViewById(R.id.webView00);
btnGo = (Button) findViewById(R.id.go_btn);
etUrl = (EditText) findViewById(R.id.url_edittext);
webView00.getSettings().setJavaScriptEnabled(true);
webView00.setWebViewClient(new myWebViewClient());
webView00.setWebChromeClient(new myWebChromeClient());
btnGo.setOnClickListener(btnGoListener);
webView00.loadUrl("http://google.com");
}
public class myWebChromeClient extends WebChromeClient {
}
public class myWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Toast.makeText(getBaseContext(), "Loading Finished", Toast.LENGTH_LONG)
.show();
}
};
From the documentation :
When onPageFinished() is called, the rendering picture may not be updated yet.
To get the notification for the new Picture, use onNewPicture(WebView, Picture).
http://developer.android.com/reference/android/webkit/WebViewClient.html#onPageFinished(android.webkit.WebView, java.lang.String)
I suggest that you implement method onProgressChanged from WebChromeClient.
Documentation :
http://developer.android.com/reference/android/webkit/WebChromeClient.html#onProgressChanged(android.webkit.WebView, int)
Have not tried the Chrome client yet, so have commented that one out, perhaps you can change seamlessly between them.
Nevertheless, I would think something like this should work:
OnClickListener btnGoListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
webView00.loadUrl(etUrl.getText().toString());
webView00.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
Toast.makeText(getBaseContext(), "Loading Finished 2", Toast.LENGTH_LONG)
.show();
}
});
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_web_page00);
webView00 = (WebView) findViewById(R.id.webView00);
btnGo = (Button) findViewById(R.id.go_btn);
etUrl = (EditText) findViewById(R.id.url_edittext);
webView00.getSettings().setJavaScriptEnabled(true);
btnGo.setOnClickListener(btnGoListener);
webView00.loadUrl("http://google.com");
//webView00.setWebViewClient(new myWebViewClient());
//webView00.setWebChromeClient(new myWebChromeClient());
webView00.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
Toast.makeText(getBaseContext(), "Loading Finished 1", Toast.LENGTH_LONG)
.show();
}
});
}
I noticed onPageFinished is sometimes called multiple times if the webview re-rendered a page that was already loaded. On the reload, the webview would first render about:blank and calls onPageFinished before loading the actual webpage and calling onPageFinished again. The sequence is:
load www.google.com
onPageFinished is called
load 'www.google.com' again
onPageFinished is called. The URL is about:blank
onPageFinished is called. The URL is www.google.com
To get around this issue in step 4, you can check that the url parameter passed to onPageFinished is the one you care about:
#Override
protected void onCreate(Bundle savedInstanceState) {
...
webView00.loadUrl(myWebPage);
}
public class myWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, URL);
if(url == myWebPage) {
Toast.makeText(getBaseContext(), "Loading Finished", Toast.LENGTH_LONG)
.show();
}
};
I am experimenting with the loopj package. I am trying to make a HTTP request to a website and display the website in the webview.
I am successfully getting a result back, however the web view does not display the page as desired, instead chrome opens up and displays the page.
Am I missing something or is there a way I can override this unwanted behaviour?
Below is my oncreate method where I am making the request:
public class MainActivity extends Activity {
Button connectBtn;
TextView status;
WebView display;
String url = "http://www.google.com";
AsyncHttpClient client;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = (TextView)findViewById(R.id.statusbox);
connectBtn = (Button)findViewById(R.id.connectBtn);
display = (WebView)findViewById(R.id.webView1);
connectBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
client = new AsyncHttpClient();
client.get(url, new AsyncHttpResponseHandler(){
#Override
public void onSuccess(String response) {
Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_SHORT).show();
display.loadUrl(url);
}
});
}
});
}
setWebViewClient to your WebView and override shouldOverrideUrlLoading() now write view.loadUrl(url); in that method.
Just add this code,
display.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}});
You need to set a WebViewClient and override the shouldOverrideUrlLoading method. Something like this:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
view.loadUrl(url);
}
});
That makes sure that clicks on links in the WebView are handled by the WebView itself.
Edit: Actually, I misread the question. You aren't dealing with a click in the WebView itself, so this isn't relevant. Sorry!
just use this code under you webviewclient
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
final EditText editText = (EditText) findViewById(R.id.urlfield);
editText.setText(url);
}
}
I am new to android.I am using progress dialog in my app but it could not stop.In my app i am using webview.In first activity I have connect button.In second activity connect to facebook code is available.In second activity code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.facebook);
wvFacebook.setWebViewClient(new MyWebViewClient());
wvFacebook.loadUrl(strFacebook);
}
private class MyWebViewClient extends WebViewClient
{
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
prDialog = ProgressDialog.show(getParent(), "In progress", "Loading, please wait...");
}
#Override
public void onPageFinished(WebView view, String url) {
Log.e("status","calling....");
super.onPageFinished(view, url);
prDialog.dismiss();
Log.e("status","calling next......");
}
}
when connect button clicked the second activity will be executed and getting facebook page.But progress dialog could not stop.
Try like this
First create dialog in your onCreate()
ProgressDialog progressDialog =new ProgressDialog(getApplicationContext());
progressDialog.setMessage("Please Wait");
Then show the dialog in onPageStarted() method.
progressDialog .show();
Then dismiss it in your onPageFinished() method
progressDialog.cancel();
You should not create Dialog in onPageStart, when loading a web page, onPageStart() could be called multi times.
In you onPageStart, you create a new Dialog without closing the old one, so the old dialog will always show.
To solve this you have many choices:
As lmran said, create the dialog only once in onCreate(). Show it in onPageStarted and dismiss in onPageFinished.
Before create a new Dialog, dismiss the old one.
just this code used and try it out,
#Override
public void onPageFinished(WebView view, String url) {
Log.e("status","calling....");
super.onPageFinished(view, url);
if (prDialog != null || prDialog.isShowing())
prDialog.dismiss();
Log.e("status","calling next......");
}
#Override
public void onPageFinished(WebView view, String url) {
try{
if (prDialog.isShowing()|| prDialog!= null) {
prDialog.dismiss();
prDialog= null; /*** Add ***/
}
}catch(Exception exception){
exception.printStackTrace();
}
}
}
You may also visit the following link to get a complete detail...
http://androiddubd.blogspot.com/2014/07/how-to-show-progress-dialog-or-loader.html