Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm not able to find whats wrong with this code. Someone please help me out. I used same code in other project as well and in that project it worked fine. I'm just trying to open a URL to load it in a WebView and I already gave internet permission in the manifest file.
Here's the code:
package com.example.itmuantiragging;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
#SuppressLint("NewApi")
public class Fragment_Website extends Fragment {
WebView wv;
ProgressDialog pg;
Background bg;
public Fragment_Website() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_website, container,false);
//Toast.makeText(getActivity(), "Loading... Please Wait..",Toast.LENGTH_LONG).show();
wv = (WebView) rootView.findViewById(R.id.webView1);
bg = new Background();
bg.execute();
return rootView;
}
public void progress(){
pg = new ProgressDialog(getActivity());
pg.setTitle("");
pg.setMessage("Please Wait.........");
pg.setCancelable(false);
pg.setIndeterminate(true);
pg.show();
}
class Background extends AsyncTask<Void, Void, Void>
{
#SuppressLint("SetJavaScriptEnabled")
#Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
try{
wv.loadUrl("http://www.siddharth.uphero.com/MTNL_address_locator.html");
}catch(Exception e){
e.printStackTrace();
}
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
wv.getSettings().setBuiltInZoomControls(true);
wv.setWebViewClient(new MyWebClient());
return null;
}
#Override
protected void onPostExecute(Void result) {
}
#Override
protected void onPreExecute() {
progress();
}
}
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);
}
#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);
pg.dismiss();
}
}
}
You can only update views on the UI thread.
I don't understand why you are doing background operation as WebView has it's own method to handle it. You use WebView right so you can directly load URL in android WebView using the following method.
webview.loadUrl(url)
So now question is how to show progress bar. Ok there is build in method onPageStarted() and onPageFinished() where you can start progress and stop progress. Move the following code inside onCreateView() method.
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
wv.getSettings().setBuiltInZoomControls(true);
wv.setWebViewClient(new MyWebClient());
Hope it make sense.
Related
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
package net.sourceforge.zbar.android.CameraTest;
import android.annotation.SuppressLint;
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 WebViewActivity extends Activity {
private WebView webView;
#SuppressLint("SetJavaScriptEnabled")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
String mUrl = getIntent().getStringExtra("mUrl");
webView = (WebView) findViewById(R.id.web_engine);
webView.setWebViewClient(new MyWebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(mUrl);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webView.canGoBack()){
webView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
class MyWebClient extends WebViewClient {
#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
return false;
}
}
I tried putting a progress dialog in this code but the app stops. I put it in the webclient class but after i ran the app the progress dialog appeared but the app stops. I already deleted that part so you can see the working code. How can I do it?
You have to add a ProgressBar object in your layout, but hidden. Then, when a page starts loading, you show the progress bar:
ProgressBar progress = (ProgressBar) findViewById(R.id.progress);
progress.setVisibility(View.VISIBLE);
You can even override the progress change
#Override
public void onProgressChanged(WebView view, int percentage) {
ProgressBar progress = (ProgressBar) findViewById(R.id.progress);
progress.setProgress(percentage);
}
For this last method, you have to extend WebChromeClient.
Progress Bar is not dismissing even though Page loading is finished
I'm trying to load the url in the webview in background so that user doesn't able to recognize that it is some other website that opens.. I want them to think that the whatever is opening in the webview is the part of the application itself. Therefore I'm trying to open url in background and till the time url loads I want progress bar to appear but as page loaded I'm dismissing progress bar but it continues to appear.. it is not dismissing, thats the problem.. please help me out
Here is my code...
package com.example.urlopeningwithprogressbar;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
ProgressDialog pg;
Background bg;
WebView wv;
Button b1;
static boolean buttonpressed=false;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button1);
wv = (WebView)findViewById(R.id.webView1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
bg = new Background();
bg.execute();
}
});
}
public void progress(){
pg = new ProgressDialog(MainActivity.this);
pg.setTitle("");
pg.setMessage("Please Wait.........");
pg.setCancelable(false);
pg.setIndeterminate(true);
pg.show();
}
class Background extends AsyncTask<Void, Void, Void>
{
#SuppressLint("SetJavaScriptEnabled")
#Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
try{
wv.loadUrl("URL");
}catch(Exception e){
e.printStackTrace();
}
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
wv.getSettings().setBuiltInZoomControls(true);
// wv.setWebViewClient(new ourViewClient());
return null;
}
#Override
protected void onPostExecute(Void result) {
Toast.makeText(MainActivity.this, "Process Complete", Toast.LENGTH_SHORT).show();
}
#Override
protected void onPreExecute() {
Toast.makeText(getApplicationContext(), "pre execute is working??", Toast.LENGTH_SHORT).show();
progress();
}
}
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);
}
#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);
pg.dismiss();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
You are never instantiating or using your myWebClient class.
You have to
wv.setWebViewClient(new myWebClient());
to actually use it.
Also note that class names should be upper case.
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'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);
}
}
});