How to add ProgressDialog while loading content from web view - java

This is my program to load a webpage into a web view. My problem is how to add ProgressDialog while loading content from web view.
public class Openpage extends Activity {
WebView wb=null;
String s1=null;
#SuppressLint({ "SetJavaScriptEnabled", "HandlerLeak" })
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newsfeed);
Intent i=getIntent();
s1=i.getStringExtra("link");
Toast.makeText(this, s1, Toast.LENGTH_LONG).show();
Log.d(s1, "hai");
ProgressDialog progress = new ProgressDialog(this);
progress.setMessage("Loading...");
new MyTask(progress).execute();
}
public class MyTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog progress;
public MyTask(ProgressDialog progress) {
this.progress = progress;
}
public void onPreExecute() {
progress.show();
}
public Void doInBackground(Void...voids) {
wb=(WebView)findViewById(R.id.webView1);
wb.loadUrl(s1);
return null;
}
public void onPostExecute(Void void1) {
progress.dismiss();
}
}

You can get to know how much the page has loaded from
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if(progress == 100){
// Page has fully loaded. Cancel the progress dialog here
}
}
});
You can read it from here

Related

Progress Dialog impossible to show using one AsyncTask class

I'm trying to show one Progress Dialog inside my MainActivity but is impossible:
This class is inside My MainActivity:
class MyAsyncTask extends AsyncTask<Void, Integer, Void> {
ProgressDialog progressDialog;
private String titulo;
private String mensaje;
private MainActivity activity;
public MyAsyncTask(MainActivity activity){
this.activity = activity;
}
#Override
protected Void doInBackground(Void... voids) {
this.activity.createDirs();
this.activity.copyAssets();
return null;
}
#Override
protected void onPreExecute() {
this.progressDialog = new ProgressDialog(MainActivity.this);
this.progressDialog.setTitle("test");
this.progressDialog.setMessage("test message...");
this.progressDialog.show();
Toast.makeText(MainActivity.this, "OPA", Toast.LENGTH_LONG).show();
super.onPreExecute();
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
}
}
This code is inside My MainActivity.java: Basically when the user press one button I'm creating one reference to my MyAsyncTask and executing it.
btnInit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(flag==1){
myAsyncTask = new MyAsyncTask(new MainActivity());
myAsyncTask.execute();
try {
myAsyncTask.get();
}catch (Exception e){
}
Intent intent = new Intent(MainActivity.this, Dashboardactivity.class);
startActivity(intent);
flag=0;
}else{
Toast.makeText(MainActivity.this,"error", Toast.LENGTH_LONG).show();
}
}
The code of the method doInBackground() of the class MyAsyncTask is being executed without problems. I will appreciate any idea guys, thanks so much.
Your thread is blocked, when you call myAsyncTask.get() that's why your Progress Dialog is not displayed so remove the myAsyncTask.get() and add those line on onPostExecute
Intent intent = new Intent(MainActivity.this, Dashboardactivity.class);
startActivity(intent);
full code :
btnInit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(flag==1){
myAsyncTask = new MyAsyncTask(new MainActivity());
myAsyncTask.execute();
flag=0;
}else{
Toast.makeText(MainActivity.this,"error", Toast.LENGTH_LONG).show();
}
}
and on your asyncTask :
class MyAsyncTask extends AsyncTask<Void, Integer, Void> {
ProgressDialog progressDialog;
private String titulo;
private String mensaje;
private MainActivity activity;
public MyAsyncTask(MainActivity activity){
this.activity = activity;
}
#Override
protected Void doInBackground(Void... voids) {
this.activity.createDirs();
this.activity.copyAssets();
return null;
}
#Override
protected void onPreExecute() {
this.progressDialog = new ProgressDialog(MainActivity.this);
this.progressDialog.setTitle("test");
this.progressDialog.setMessage("test message...");
this.progressDialog.show();
Toast.makeText(MainActivity.this, "OPA", Toast.LENGTH_LONG).show();
super.onPreExecute();
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
Intent intent = new Intent(MainActivity.this, Dashboardactivity.class);
startActivity(intent);
}
}

Loading created by ProgressDialog wouldn't stop , how to hide progressbar when page is loaded

I'm new to Java and was trying to create a web-view app. The loading widget was created using Progress Dialog but it wouldn't stop. I don't know what is wrong. Please help.
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...",true);
mywebView = (WebView) findViewById(R.id.webview);
mywebView.getSettings().setJavaScriptEnabled(true);
mywebView.getSettings().setSupportZoom(true);
mywebView.getSettings().setBuiltInZoomControls(true);
mywebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if(pd!=null && pd.isShowing())
{
pd.dismiss();
}
}
});
mywebView.loadUrl("https://google.com");
mywebView.setWebViewClient(new WvClient());
}
private class WvClient extends WebViewClient
{
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError er) {
handler.proceed();
}
}
#Override
public void onBackPressed() {
if(mywebView.canGoBack())
{
mywebView.goBack();
}
else
{
super.onBackPressed();
}
}
}
You have declared setWebViewClient twice , ie after load url you again
declared the setWebViewClient with your custom client , correct answer is
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...",true);
mywebView = (WebView) findViewById(R.id.webview);
mywebView.getSettings().setJavaScriptEnabled(true);
mywebView.getSettings().setSupportZoom(true);
mywebView.getSettings().setBuiltInZoomControls(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
pd.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(pd!=null && pd.isShowing())
{
pd.dismiss();
}
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
if(pd!=null && pd.isShowing())
{
pd.dismiss();
}
}
});
mywebView.loadUrl("https://google.com");
}
#Override
public void onBackPressed() {
if(mywebView.canGoBack())
{
mywebView.goBack();
}
else
{
super.onBackPressed();
}
}
}
Place a breakpoint in
onPageFinished
and see if the listener actually get called?

android create ProgressDialog in constructor

I created Activity that includes ProgressDialog and its methods.
public class ProgressActivity extends AppCompatActivity implements Thread.UncaughtExceptionHandler {
public final ProgressDialog progressDialog;
public void dismissPD() {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
public void setPDMessage(String msg) {
progressDialog.setMessage(msg);
}
public void showPD(String msg) {
ProgressDialog.show(this, "", msg);
}
#Override
public void uncaughtException(Thread thread, Throwable ex) {
ex.printStackTrace();
}
public ProgressActivity() {
progressDialog = new ProgressDialog(this);
}
#Override
protected void onDestroy() {
super.onDestroy();
dismissPD();
}
}
Now I extend my new activity from it, but I get NullpointerException inside constructur on progressDialog = new ProgressDialog(this)
As I see, I have a wrong context when constructor runs. Is there a right approach to realize it in my way?
I solved the problem by overriding oncreate mothod of the main Activity:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
progressDialog = new ProgressDialog(this);
}

java.lang.IllegalStateException: Fragment not attached to Activity issue in android

I am facing strange issue during call of my activity with Fragment usage. I am getting error like,
java.lang.IllegalStateException: Fragment ScoreFragment{ee2b833
id=0x7f0e0198} not attached to Activity
On line 146. My Fragment code which have error is line like below
if(mPageFlag.equalsIgnoreCase(getString(R.string.winners))){
And My full code for same is below,
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mPageFlag = getArguments().getString(ARG_PAGE_FLAG);
}
}
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser && !mIsPageLoaded){
mContext = getActivity();
mIsPageLoaded = true;
if(mPageFlag.equalsIgnoreCase(getString(R.string.winners))){
new getcontestscorewinners(mContext).execute();
}else{ //
new getcontestscorewinnersNew(mContext).execute();
}
}
}
public class getcontestscorewinners extends AsyncTask<String, Void, String> {
boolean response = false;
private Context mContext;
public getcontestscorewinners(Context context) {
mContext = context;
}
#Override
protected void onPreExecute() {
progress = ProgressDialog.show(mContext, "Processing...",
"Please wait....");
}
#Override
protected String doInBackground(String... params) {
NetworkTask.getContestScoreWinners(winnerHandler);
return "";
}
#Override
protected void onPostExecute(String result) {
}
#Override
protected void onProgressUpdate(Void... values) {
}
}
Let me know if someone can help me for get out of it. Thanks.
Try using isAdded():
Return true if the fragment is currently added to its activity.
So your code should be like this :
if(isAdded() && mPageFlag.equalsIgnoreCase(getString(R.string.winners))){

The method that works only in one place, progressdialog

I have a method that takes lat/lng from mysql and puts the markers on the map.
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
myMethod();
}
});
Everything in this way works.
But when i try to put this method with ProgressDialog markers do not appear. After push the button i see only "loading..." and "Done".
This is a way that does not work:
I
pg = new ProgressDialog(this);
pg.setMessage("wait...");
II
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
pg.show();
new BackgroundJob().execute();
Toast.makeText(MapsActivity.this, "loading...", Toast.LENGTH_SHORT).show();
}
});
III
private class BackgroundJob extends AsyncTask<Void, Void, Void>
{
#Override
protected Void doInBackground(Void... params) {
myMethod();
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
Toast.makeText(MapsActivity.this, "Done", Toast.LENGTH_SHORT).show();
pg.cancel();
}
}
what is wrong here?

Categories