Skip to content

Instantly share code, notes, and snippets.

@tbruyelle
Created January 15, 2014 15:05
Show Gist options
  • Save tbruyelle/8437894 to your computer and use it in GitHub Desktop.
Save tbruyelle/8437894 to your computer and use it in GitHub Desktop.
smooth progress bar in action bar
<?xml version="1.0" encoding="utf-8"?>
<fr.castorflex.android.smoothprogressbar.SmoothProgressBar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:minHeight="@dimen/progress_bar_height"
app:spb_sections_count="6"
app:spb_color="@color/orange"
app:spb_speed="1.0"
app:spb_stroke_width="@dimen/progress_bar_height"
app:spb_stroke_separator_length="4dp"
app:spb_reversed="false"
app:spb_mirror_mode="false"
/>
package util;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import com.R;
import com.nineoldandroids.view.ViewHelper;
public class ProgressBarWrapper {
private final boolean mAllowFade;
ProgressBar mProgressBar;
public ProgressBarWrapper(Context context, View decorView) {
mAllowFade = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
mProgressBar = (ProgressBar) LayoutInflater.from(context).inflate(R.layout.progressbar, null, false);
mProgressBar.setVisibility(View.GONE);
if (mAllowFade) {
ViewHelper.setAlpha(mProgressBar, 0f);
}
// get the action bar layout
int actionBarId = context.getResources().getIdentifier("action_bar_container", "id", "android");
FrameLayout f = (FrameLayout) decorView.findViewById(actionBarId);
if (f == null) {
f = (FrameLayout) decorView.findViewById(com.actionbarsherlock.R.id.abs__action_bar_container);
}
// add the view to that layout
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM;
f.addView(mProgressBar, params);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void show() {
mProgressBar.setVisibility(View.VISIBLE);
if (mAllowFade) {
ObjectAnimator.ofFloat(mProgressBar, "alpha", 0f, 1f).start();
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void hide() {
if (mAllowFade) {
ObjectAnimator anim = ObjectAnimator.ofFloat(mProgressBar, "alpha", 1f, 0f);
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mProgressBar.setVisibility(View.GONE);
}
});
anim.start();
} else {
mProgressBar.setVisibility(View.GONE);
}
}
}
@explodes
Copy link

explodes commented Jul 1, 2014

This was useful for me, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment