-2

Can anyone help me to add a progress bar?
I have added various progress bar methods to my code but the progress bar does not end after the page is successfully loaded.

This is my MainActivity.java:

package com.myappname.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.pushbots.push.Pushbots;

public class MainActivity extends Activity {
    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Pushbots.sharedInstance().init(this);

        mWebView = (WebView) findViewById(R.id.activity_main_webview);

        // Force links and redirects to open in the WebView instead of in a browser
        mWebView.setWebViewClient(new WebViewClient());

        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        // Use remote resource
        mWebView.loadUrl("http://www.mysiteurl.com");

        // Stop local links and redirects from opening in browser instead of WebView
        mWebView.setWebViewClient(new MyAppWebViewClient());

        // Use local resource
        // mWebView.loadUrl("file:///android_asset/www/index.html");
    }

    // Prevent the back-button from closing the app
    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            new AlertDialog.Builder(this)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setTitle("Alert")
                    .setMessage("Are you sure you want exit?")
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }

                    })
                    .setNegativeButton("No", null)
                    .show();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

This is my activity_main.xml in layout:

<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">

<WebView
    android:id="@+id/activity_main_webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

bcsb1001
  • 2,834
  • 3
  • 24
  • 35

1 Answers1

0

It is recommended to use a progress dialog inside AsyncTask. So first declare a progress dialog :

ProgressDialog progressDialog;

Initialize it :

this.progressDialog = new ProgressDialog(context);

Then inside PreExecute :

progressDialog.setMessage("Loading");


progressDialog.show();

and inside PostExecute :

 progressDialog.dismiss();
Akriti
  • 773
  • 1
  • 10
  • 19
  • As this is my first app so i don't have much knowledge about it, can you please implement progress bar code in my above code? – Angel Web Jul 03 '16 at 08:01
  • You will have to implement AsyncTask first. Try this https://developer.android.com/reference/android/os/AsyncTask.html – Akriti Jul 04 '16 at 20:47
  • just for an example you can have a look at this : https://github.com/akritikts/TravelPlanner/tree/master/app/src/main/java/in/silive/travelplanner/Hotels – Akriti Jul 04 '16 at 20:54