Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionBar is not showing the progress bar

I have the WebView and I want to while page is loading in the WebView, in ActionBar show progress. The application is using AppCompat Android library and the app's target and compile SDK version is 21. Just the WebView running normally.

I think problem in the method setSupportProgress(int progress).

My code is the below.

Activity:

public class AuthActivity extends ActionBarActivity {

private boolean isConfirmationRequired = false;
private Utils utils = new Utils();

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
    supportRequestWindowFeature(Window.FEATURE_PROGRESS);
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_auth);

    final String authUrl = "some url";
    final WebView authWebView = (WebView) findViewById(R.id.auth_web_view);
    authWebView.getSettings().setJavaScriptEnabled(true);
    authWebView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            setTitle(R.string.loading);
            setSupportProgress(newProgress * 100);

            if (newProgress == 100)
                setTitle(R.string.sign_in);
        }
    });

    authWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            if (!url.equals(authUrl))
                isConfirmationRequired = true;
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView webView, String url) {
            boolean hasUserBrokenAuth = url.equals("some url") ||
                    url.equals("some url");
            boolean isUserAuthorized = url.startsWith("some url" +
                    "access_token=");

            if (hasUserBrokenAuth) {
                if (isConfirmationRequired)
                    utils.showConfirmDialog(AuthActivity.this);
                else
                    finish();
            } else {
                utils.webViewLoadUrlIfInternetIsConnected(AuthActivity.this, authWebView, url);
                if (isUserAuthorized)
                    saveAuthData(url);
            }
            return true;
        }
    });

    utils.webViewLoadUrlIfInternetIsConnected(this, authWebView, authUrl);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        if (isConfirmationRequired) {
            utils.showConfirmDialog(this);
        }
    }
    return super.onKeyDown(keyCode, event);
}

@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.
    if (isConfirmationRequired) {
        utils.showConfirmDialog(this);
        return true;
    } else {
        return super.onOptionsItemSelected(item);
    }

}

private void saveAuthData(String url) {
    String accessToken = utils.extractPattern(url, "access_token=(.*?)&");
    String userId = utils.extractPattern(url, "user_id=(\\d*)");
}

utils.webViewLoadUrlIfInternetIsConnected:

public void webViewLoadUrlIfInternetIsConnected(final Context context, final WebView webView,
                                                final String url) {
    if (isInternetConnected(context)) {
        webView.loadUrl(url);
    } else {
        showSnackbarInternetDisconnected(context, new ActionClickListener() {
            @Override
            public void onActionClicked(Snackbar snackbar) {
                snackbar.dismiss();
                webViewLoadUrlIfInternetIsConnected(context, webView, url);
            }
        });
    }
}
like image 710
Alex Andro Avatar asked Oct 20 '22 18:10

Alex Andro


1 Answers

AppCompat does not support showing a progress view as it was possible, so you can remove following from your code:

supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setSupportProgressBarIndeterminateVisibility(...);

There is the possibility to use the new toolbar instead, I tried it by myself today, but it complicates all. The solution I ended with was to set a menu item more that references a layout which references a drawbale:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" >

<item
    android:id="@+id/menu_progress"
    android:visible="false"
    app:showAsAction="always"
    app:actionLayout="@layout/progress_indeterminate"
    tools:ignore="AlwaysShowAction,UnusedAttribute"/>

Then the layout looks like:

<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar_progress_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminateDrawable="@drawable/custom_progress_wheel" />

And the drawable as follow (you can customize this as you want):

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="720" >

<shape
    android:innerRadiusRatio="3"
    android:shape="ring"
    android:thicknessRatio="12"
    android:useLevel="false" >

    <gradient
        android:centerColor="#FFFFFF"
        android:centerY="0.50"
        android:endColor="#FFFFFF"
        android:startColor="#000000"
        android:type="sweep"
        android:useLevel="false" />
</shape>
</rotate>

Then in onCreateOptionsMenu you initialize the menuItem with following:

mProgressMenu = menu.findItem(R.id.menu_progress);

and with setVisibility you can chage the visibility of the menu anywhere in your activity.

like image 56
David Avatar answered Oct 22 '22 09:10

David