Android - An action bar item which acts both as a refresh button and as a progress indicator

Overview

RefreshActionItem

An action bar item that implements this common pattern:

  • Initially it shows a refresh button.
  • If the button is clicked, a background operation begins and the button turns into a progress indicator.
  • When the background operation ends, the button is restored to its initial state.

The progress bar shows a magnitude which represents how far the operation has proceeded. The progress bar can also be made indeterminate, just like the built-in ProgressBar.

It is possible to add a small badge to the action item. This tells the user that there is new data available.

This library requires ActionBarSherlock, and is thus compatible with Android 2.x and newer. If you use the new ActionBarCompat instead, you can use this fork. Finally, if you don't need 2.x compatibility and thus use the native action bar, there is another fork that you can use.

Example Image

Try out the sample application:

Android app on Google Play

Or browse the source code of the sample application for a complete example of use.

Including in your project

If you’re using Eclipse with the ADT plugin you can include RefreshActionItem as a library project. Create a new Android project in Eclipse using the library/ folder as the existing source. Then, open the properties of this new project and, in the 'Android' category, add a reference to the ActionBarSherlock library project. Finally, in your application project properties, add a reference to the created library project.

If you use maven to build your Android project you can simply add a dependency for this library.

 <dependency>
     <groupId>com.github.manuelpeinado.refreshactionitem</groupId>
     <artifactId>library</artifactId>
     <version>1.0.3</version>
     <type>apklib</type>
 </dependency>

Usage

Add an element for the refresh action to your XML menu:

<item
    android:id="@+id/refresh_button"          
    android:actionViewClass=
        "com.manuelpeinado.refreshactionitem.RefreshActionItem"
    android:showAsAction="always"
    android:title="@string/action_refresh"/>

Then, configure the action in the onCreateOptionsMenu method of your SherlockActivity-derived activity:

@Override public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.main, menu);
    MenuItem item = menu.findItem(R.id.refresh_button);
    mRefreshActionItem = (RefreshActionItem) item.getActionView();
    mRefreshActionItem.setMenuItem(item);
    mRefreshActionItem.setMax(100);
    mRefreshActionItem.setRefreshActionListener(this);
    return true;
}

The setRefreshActionListener method registers a callback that will be invoked when the refresh button is clicked. Start your background process from this callback and invoke showProgress(true) on the action item so that it turns into a progress indicator:

@Override
public void onRefreshButtonClick(RefreshActionItem sender) {
    mRefreshActionItem.showProgress(true);
    startBackgroundTask();
}

From your background task, call the action item's setProgress(int) method each time some progress is made:

mRefreshActionItem.setProgress(progress);

Finally, when the background task is complete restore the action item to its original state:

mRefreshActionItem.showProgress(false);

Progress indicator types

By default the action item shows the amount of progress using a wheel. There is an additional style, "pie", which you can activate by calling setProgressIndicatorType(ProgressIndicatorType.PIE) on your action item.

Also, if the progress of your background task cannot be easily measured you might prefer to use an indeterminate progress indicator. To achieve this just pass ProgressIndicatorType.INDETERMINATE to setProgressIndicatorType().

Badges

Sometimes it is useful to give the user a visual hint suggesting that there is new data to be loaded. You can easily achieve this by adding a badge to your action item:

mRefreshActionItem.showBadge();

The badge shows an exclamation mark by default, but you can specify an alternative text if you desire.

Customization

You can easily customize the appearance of your RefreshActionItems. Just define a refreshActionItemStyle attribute in your theme and make it reference a custom style where you specify new values for any of the multiple attributes recognized by the library.

The following snippet is extracted from the accompanying sample application. To see it in action open the "Styling" demo in the main menu.

<style name="AppTheme" parent="Theme.Sherlock.Light">
    <item name="refreshActionItemStyle">@style/CustomRefreshActionItem</item>
</style>

<style name="CustomRefreshActionItem" parent="Widget.RefreshActionItem.Light">
    <item name="progressIndicatorType">pie</item>
    <item name="badgeBackgroundColor">#A4F4</item>
    <item name="badgeTextStyle">@style/BadgeText</item>
    <item name="badgePosition">bottomLeft</item>
</style>

<style name="BadgeText">
    <item name="android:textSize">14dp</item>
    <item name="android:textColor">#7000</item>
</style>

Libraries used

Credits

Who's using it

  • The New York Times. Experience the world’s finest journalism with The New York Times app for Android.
  • DevAppsDirect. Developer Apps Direct is a large and growing collection of library demos.
  • Signos Fodas. With this app you can follow all horoscope signs, updated daily in real time on your Android (Portuguese only).

Does your app use RefreshActionItem? If you want to be featured on this list drop me a line.

Developed By

Manuel Peinado Gallego - [email protected]

Follow me on Twitter Follow me on Twitter Follow me on Twitter

License

Copyright 2013 Manuel Peinado

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Comments
  • ShowBadge generates FrameLayoutes indefinitely

    ShowBadge generates FrameLayoutes indefinitely

    Everytime I use showBadge(String text) it places FrameLayout inside of FrameLayout leading eventually to StackOverflow error. You can confirm it with HierarchyViewer tool. Potential fix is to remove all views before adding new views in BadgeView class like that: (untested with other methods, seems to work with showBadge(String text)):

    } else {
    
            // TODO verify that parent is indeed a ViewGroup
            ViewGroup group = (ViewGroup) parent; 
            int index = group.indexOfChild(target);
            group.removeAllViews();
            group.addView(target);
    
            this.setVisibility(View.GONE);
            group.addView(this);
            group.invalidate();
    
        }
    
    opened by Mroczny 1
  • Update minSdkVersion to match ActionBarSherlock

    Update minSdkVersion to match ActionBarSherlock

    Since the project requires ActionBarSherlock, the minSdkVersion shouldn't be less than the minSdkVersion of ActionBarSherlock.

    Probably not completely necessary but it was causing my gradle build to complain about version numbers when it tried to merge the manifests.

    opened by intrications 0
  • Need a package without support library included

    Need a package without support library included

    Hi, it would be great if you could redistribute the package without the support library included and only referenced through Maven. As is today, it generates conflicts with other packages that uses this support library (in gradle builds).

    opened by pernilla 0
  • Need an AAR package in Maven central

    Need an AAR package in Maven central

    It would be great to get an AAR of this in the maven central repo so we can use this inside Android Studio projects.

    Theres a good resource here for how to package it up and submit: http://gmariotti.blogspot.it/2013/09/publish-aar-file-to-maven-central-with.html

    opened by Wavesonics 6
  • how to addSubMenu ?

    how to addSubMenu ?

    hi man! i mean how can i to add the menu item just like this :+1: SubMenu rootMenu = menu.addSubMenu(""); MenuItem rootMenuItem = rootMenu.getItem(); rootMenuItem.setIcon(R.drawable.actionbar_icon_overflow); rootMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); rootMenu.add(android.view.Menu.NONE, 1, android.view.Menu.NONE, "1"); rootMenu.add(android.view.Menu.NONE, 2, android.view.Menu.NONE, "2"); when i select "1",i need the top bar show progress ,just like your StyledActivity! ( mSaveButton.showProgress(true);)

    opened by liufsd 4
  • RefreshActionItem object in activity is null

    RefreshActionItem object in activity is null

    Hi Manuel Peinado,

    I'm trying to use this library, I followed your instructions to add this library in my project but I'm experiencing an error in onCreateOptionsMenu method. This is the body of my onCreateOptionsMenu method:

        getSupportMenuInflater().inflate(R.menu.activity_home, menu);
    
    MenuItem item = menu.findItem(R.id.refresh_menu);
    
    if (item != null) {
        mRefreshActionItem = (RefreshActionItem) item.getActionView();
        mRefreshActionItem.setMenuItem(item);
        mRefreshActionItem.setMax(100);
        mRefreshActionItem.setRefreshActionListener(this);
        loadData();
    }
    
    //  return super.onCreateOptionsMenu(menu);
    return true;
    

    and I get the NullPointerException in the following line:

    mRefreshActionItem.setMenuItem(item);.

    As you can see, only the mRefreshActionItem object can be null. My question is: why do I get this exception???

    opened by devilmac 13
  • Problem compiling the project with IntelliJ IDEA 12

    Problem compiling the project with IntelliJ IDEA 12

    java: /Users/menor/workspace android/RefreshActionItem-master/library/gen/com/manuelpeinado/refreshactionitem/R.java:6: duplicate class: com.manuelpeinado.refreshactionitem.R

    I've read something about Maven. I think it's maven stuff...

    Related links: https://github.com/excilys/androidannotations/issues/423

    opened by cesards 0
Owner
Manuel Peinado Gallego
Manuel Peinado Gallego
IOSProgressBar is a progress-bar lib for android. And the progress-bar looks like iOS system style

IOSProgressBar is a progress-bar lib for android. And the progress-bar looks like iOS system style

heyangyang 6 Aug 25, 2022
A progress wheel for android, intended for use instead of the standard progress bar.

Deprecation warning This project is no-longer maintained, and has not been maintained for a few years now. If you're looking for an alternative librar

Todd Davies 2.7k Dec 29, 2022
A loading animation based on Floating Action Button

FAB-Loading A loading animation based on Floating Action Button. Usage Include the LoadingView widget in your view: <io.saeid.fabloading.LoadingView

Saeed Masoumi 689 Oct 14, 2022
Android AlertDialog with moving dots progress indicator

Spots progress dialog Android AlertDialog with moving spots progress indicator packed as android library. =========== Usage The library available in m

Maksym Dybarskyi 1.1k Dec 26, 2022
Progress Button is a android library for hanling different types state like active, finished, enabled, disabled and reset with a single line of code.

Progress Button is a android library for hanling different types state like active, finished, enabled, disabled and reset with a single line of code.

Sagar Khurana 38 Nov 15, 2022
A customizable, animated progress bar that features rounded corners. This Android library is designed to look great and be simple to use 🎉

RoundedProgressBar Easy, Beautiful, Customizeable The RoundedProgressBar library gives you a wide range of customizable options for making progress ba

null 541 Jan 1, 2023
A progress bar with animation, gradient and colorful shadow.

Fancy Progressbar Android library providing a beautiful progressbar with colorful shadow, gradient and animation for Jetpack Compose. Download Add in

Fatemeh Afshari 5 Dec 25, 2022
[Android] Round Corner Progress Bar Library for Android

RoundCornerProgressBar Round corner is cool. Let's make your progress bar to round corner Colorful progress bar with round corner on progress which yo

Akexorcist 2.3k Dec 31, 2022
[Android] Round Corner Progress Bar Library for Android

RoundCornerProgressBar Round corner is cool. Let's make your progress bar to round corner Colorful progress bar with round corner on progress which yo

Akexorcist 2.3k Jan 7, 2023
A wave view of android,can be used as progress bar.

WaveView ![Gitter](https://badges.gitter.im/Join Chat.svg) A wave view of android,can be used as progress bar. Screenshot APK demo.apk What can be use

Kai Wang 1.3k Dec 28, 2022
DownloadProgressBar is an android library that delivers awesome custom progress bar. You can manipulate it's state in every way.

Download Progress Bar Android progress bar with cool animation, inspired by : https://dribbble.com/shots/2012292-Download-Animation ###Attributes Attr

Mariusz Brona 978 Nov 10, 2022
Open source android library for different progress bar designs

MultiProgressBar A progress bar library for Android that provides customized progress bars. Built with ❤︎ by Aseem Khare ?? Installation Add this in y

Aseem Khare 124 Nov 15, 2022
An android library to easily add circular progress bar into your Jetpack Compose apps.

CircularProgressBar for Jetpack Compose An android library to easily add circular progress bar into your Jetpack Compose apps. Have a Look Usage Circu

Hitanshu Dhawan 38 Oct 30, 2022
This is beautiful color arc progress bar.

ColorArcProgressBar 中文版 This is a customizable circular progressbar.It can achieve the effect of the QQ health's arc progress with XML. What's more, w

PASSION 928 Dec 6, 2022
A customizable indeterminate progress bar

DilatingDotsProgressBar Installation compile 'com.github.justzak:dilatingdotsprogressbar:1.0.1' Usage <com.zl.reik.dilatingdotsprogressbar.DilatingDo

Zachary Reik 628 Sep 24, 2022
Arc pointer - simple customized progress bar in the form of an arch

ArcPointer Simple customized progress bar in the form of an arch Demo Quick start Step 1 Gradle: compile 'io.github.dvegasa:arcpointer:1.0.2' Maven:

Ed Khalturin 79 Nov 22, 2022
MusicBar 2.1 0.0 Java view visualize progress bar for sound file like sound cloud

MusicBar Setup dependencies { implementation 'com.oze.music:MusicBar:1.0.5' } Usage Function Description setAnimationChangeListener(OnMusicBarAn

emad 74 Aug 26, 2022
Progress Bar in the shape of regular polygon.

N-SidedProgressBar Progress Bar in the shape of regular polygon. Download The library is available on jcenter. Just add the dependency to your build.g

Kaishu Sahu 69 Jul 25, 2022
a circle progress bar with effect

RingProgress a circle progress bar with effect #Preview ##Usage xml <com.ldoublem.ringPregressLibrary.RingProgress android:id="@+id/ring_prog

ldoublem 629 Nov 14, 2022