FileDocCategorySizeDatePackage
TextProgressBar.javaAPI DocAndroid 5.1 API7165Thu Mar 12 22:22:10 GMT 2015com.android.internal.widget

TextProgressBar

public class TextProgressBar extends android.widget.RelativeLayout implements android.widget.Chronometer.OnChronometerTickListener
Container that links together a {@link ProgressBar} and {@link Chronometer} as children. It subscribes to {@link Chronometer#OnChronometerTickListener} and updates the {@link ProgressBar} based on a preset finishing time.

This widget expects to contain two children with specific ids {@link android.R.id.progress} and {@link android.R.id.text1}.

If the {@link Chronometer} {@link android.R.attr#layout_width} is {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT}, then the {@link android.R.attr#gravity} will be used to automatically move it with respect to the {@link ProgressBar} position. For example, if {@link android.view.Gravity#LEFT} then the {@link Chronometer} will be placed just ahead of the leading edge of the {@link ProgressBar} position.

Fields Summary
public static final String
TAG
static final int
CHRONOMETER_ID
static final int
PROGRESSBAR_ID
android.widget.Chronometer
mChronometer
android.widget.ProgressBar
mProgressBar
long
mDurationBase
int
mDuration
boolean
mChronometerFollow
int
mChronometerGravity
Constructors Summary
public TextProgressBar(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr, int defStyleRes)


             
        super(context, attrs, defStyleAttr, defStyleRes);
    
public TextProgressBar(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr)

        super(context, attrs, defStyleAttr);
    
public TextProgressBar(android.content.Context context, android.util.AttributeSet attrs)

        super(context, attrs);
    
public TextProgressBar(android.content.Context context)

        super(context);
    
Methods Summary
public voidaddView(android.view.View child, int index, ViewGroup.LayoutParams params)
Catch any interesting children when they are added.

        super.addView(child, index, params);
        
        int childId = child.getId();
        if (childId == CHRONOMETER_ID && child instanceof Chronometer) {
            mChronometer = (Chronometer) child;
            mChronometer.setOnChronometerTickListener(this);
            
            // Check if Chronometer should move with with ProgressBar 
            mChronometerFollow = (params.width == ViewGroup.LayoutParams.WRAP_CONTENT);
            mChronometerGravity = (mChronometer.getGravity() &
                    Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK);
            
        } else if (childId == PROGRESSBAR_ID && child instanceof ProgressBar) {
            mProgressBar = (ProgressBar) child;
        }
    
public voidonChronometerTick(android.widget.Chronometer chronometer)
Callback when {@link Chronometer} changes, indicating that we should update the {@link ProgressBar} and change the layout if necessary.

        if (mProgressBar == null) {
            throw new RuntimeException(
                "Expecting child ProgressBar with id 'android.R.id.progress'");
        }
        
        // Stop Chronometer if we're past duration
        long now = SystemClock.elapsedRealtime();
        if (now >= mDurationBase) {
            mChronometer.stop();
        }

        // Update the ProgressBar status
        int remaining = (int) (mDurationBase - now);
        mProgressBar.setProgress(mDuration - remaining);
        
        // Move the Chronometer if gravity is set correctly
        if (mChronometerFollow) {
            RelativeLayout.LayoutParams params;
            
            // Calculate estimate of ProgressBar leading edge position
            params = (RelativeLayout.LayoutParams) mProgressBar.getLayoutParams();
            int contentWidth = mProgressBar.getWidth() - (params.leftMargin + params.rightMargin);
            int leadingEdge = ((contentWidth * mProgressBar.getProgress()) /
                    mProgressBar.getMax()) + params.leftMargin;
            
            // Calculate any adjustment based on gravity
            int adjustLeft = 0;
            int textWidth = mChronometer.getWidth();
            if (mChronometerGravity == Gravity.END) {
                adjustLeft = -textWidth;
            } else if (mChronometerGravity == Gravity.CENTER_HORIZONTAL) {
                adjustLeft = -(textWidth / 2);
            }
            
            // Limit margin to keep text inside ProgressBar bounds
            leadingEdge += adjustLeft;
            int rightLimit = contentWidth - params.rightMargin - textWidth;
            if (leadingEdge < params.leftMargin) {
                leadingEdge = params.leftMargin;
            } else if (leadingEdge > rightLimit) {
                leadingEdge = rightLimit;
            }
            
            params = (RelativeLayout.LayoutParams) mChronometer.getLayoutParams();
            params.leftMargin = leadingEdge;
            
            // Request layout to move Chronometer
            mChronometer.requestLayout();
            
        }
    
public voidsetDurationBase(long durationBase)
Set the expected termination time of the running {@link Chronometer}. This value is used to adjust the {@link ProgressBar} against the elapsed time.

Call this after adjusting the {@link Chronometer} base, if necessary.

param
durationBase Use the {@link SystemClock#elapsedRealtime} time base.

        mDurationBase = durationBase;
        
        if (mProgressBar == null || mChronometer == null) {
            throw new RuntimeException("Expecting child ProgressBar with id " +
                    "'android.R.id.progress' and Chronometer id 'android.R.id.text1'");
        }
        
        // Update the ProgressBar maximum relative to Chronometer base
        mDuration = (int) (durationBase - mChronometer.getBase());
        if (mDuration <= 0) {
            mDuration = 1;
        }
        mProgressBar.setMax(mDuration);