Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display ProgressDialog using Fragment

I have a Fragment activity that has a ListFragment on the left and a Fragment on the right that has a WebView. The functionality works fine but I would like to display a progress dialog showing "Loading..." while the web page finishes loading. How can I accomplish this seemingly trivial task?

Below is my code:

public class ArticleListActivity extends FragmentActivity implements
    ArticleListFragment.OnArticleSelectedListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.articlelist_fragment);
    }

    public void onArticleSelected(String contentLink) {
        ArticleViewerFragment viewer = (ArticleViewerFragment) getSupportFragmentManager()
                .findFragmentById(R.id.articleview_fragment);

        if (viewer == null || !viewer.isInLayout()) {
            Intent showContent = new Intent(getApplicationContext(),
                    ArticleViewerActivity.class);

            showContent.setData(Uri.parse(contentLink));
            startActivity(showContent);
        } else {
            viewer.updateUrl(contentLink);
        }
    }
}

Here is my FragmentActivity

public class ArticleViewerActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.articleview_fragment);

        Intent launchingIntent = getIntent();
        String content = launchingIntent.getData().toString();

        ArticleViewerFragment viewer = (ArticleViewerFragment) getSupportFragmentManager()
                .findFragmentById(R.id.articleview_fragment);

        viewer.updateUrl(content);
    }
}

and here is my Fragment

public class ArticleViewerFragment extends Fragment {

    private ProgressDialog progressBar; 

    private WebView viewer = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        viewer = (WebView) inflater.inflate(R.layout.webview_layout, container, false);
        viewer.loadUrl("file:///android_asset/default.html");
        return viewer;
    }

    public void updateUrl(String newUrl) {
        if (viewer != null) {
            viewer.loadUrl(newUrl);
        }
    }
}
like image 746
Byron Avatar asked Nov 14 '22 06:11

Byron


1 Answers

Use this code:

final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...", true);
pd.setCancelable(false);

webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setSupportZoom(true);
webview.getSettings().setBuiltInZoomControls(true);
webview.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        if (pd != null && pd.isShowing()) {
            pd.dismiss();
        }
    }
});
webview.loadUrl("file:///android_asset/default.html");
like image 167
Ajmal Salim Avatar answered Nov 16 '22 03:11

Ajmal Salim