Progressbar in ActionBar
if you want add progress to actionbar call this method before setlayout
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
UPDATE where should request window feature
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
.... // other code goes here
}
and call this method when you want run it
EDIT in fragment you can add getActivity()
before requestWindowFeature
to be getActivity().requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setProgressBarIndeterminateVisibility(true); // turn progress on
setProgressBarIndeterminateVisibility(false); // turn progress off
hope this work , my recommendation to use toolbar
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/material_green_500"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<ProgressBar
android:id="@+id/toolbar_progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateTint="#795548"
android:indeterminateTintMode="src_in"
android:layout_gravity="right"
/>
</android.support.v7.widget.Toolbar>
Why I recommended toolbar with ProgressBar ?
for two reasons , First setSupportProgressBarIndeterminate deprecated.
Second easy to use just set setVisibility
method to make it visible call setVisibility(View.VISIBLE);
to hide view call setVisibility(View.GONE);
Put a progressBar on ActionBar
NOTE: The functionality below is now deprecated in the Support Library.
You need to call
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS)
in your onCreate()
before setting the activity's layout:
e.g.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
... // set layout etc
If you are using the support library replace requestWindowFeature with supportRequestWindowFeature
And then call
setProgressBarIndeterminateVisibility(true);
on your Activity whenever you want to show the progress spinner.
how to put circular progress bar in action bar?
You can do it using a progress bar inside Toolbar. Toolbar is just a ViewGroup:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_main"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#2196F3"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".70"
android:gravity="center"
android:orientation="horizontal"
android:text="left">
<TextView
android:id="@+id/toolbar_title"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Tital"
android:textColor="@color/white"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight=".30"
android:text="right">
<ProgressBar
android:id="@+id/actionProgressBar"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center"
android:visibility="visible" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.Toolbar>
ProgressBar under Action Bar
This is now a native behavior that can be obtained using SwipeRefreshLayout.
You can wrap your scrollable view with a SwipeRefreshLayout
and then you just need to listen to onRefresh events:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
}
@Override public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override public void run() {
swipeLayout.setRefreshing(false);
}
}, 5000);
}
A nice and simple tutorial can be found in this blog.
How can I make a custom progress bar in the action bar?
To make the progress bar show up, you need to turn on ActionBar.DISPLAY_SHOW_CUSTOM in ActionBar.displayOptions.
Catch notes is using a custom animation for their progress bar. I think that the default Holo (normal-sized) one is a reasonable alternative, but to get something like their specific one, you'll want to create an animationDrawable.
http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html
Then get the custom view from the ActionBar with getCustomView(), find your ImageView, and start it animating.
How to add progress bar icon in action bar?
First of all you have to create a custom menu to use inside your activity.
Secondly, you have to request the feature of progress bar inside your "onCreate".
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
// rest of your code
[....]
}//end of onCreate
Then your my_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_refresh"
android:icon="@ drawable/ic_menu_refresh "
android:orderInCategory="100"
android:showAsAction="always"
android:title="Refresh"
android:visible="true"/>
</menu>
Back to your activity, you have to inflate this menu:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_refresh:
// refresh icon clicked
//show the indeterminate progress bar
setSupportProgressBarIndeterminateVisibility(true);
// Rest of your code to do re refresh here
[...]
default:
return super.onOptionsItemSelected(item);
}
}
PS: All the above code is working using the ActionBar Compat Library.
EDIT:
Ok, to hide the action bar icon do this:
-Create a menuitem outside your functions:
MenuItem refreshIcon;
Edit your onCreateOptionsMenu:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
//Initializing menu item
refreshIcon = menu.findItem(R.id.action_refresh);
return super.onCreateOptionsMenu(menu);
}
Then when you press the button change the visibility:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_refresh:
// refresh icon clicked
//show the indeterminate progress bar
setSupportProgressBarIndeterminateVisibility(true);
//hiding action icon
refreshIcon.setVisible(false);
// Rest of your code to do re refresh here
[...]
default:
return super.onOptionsItemSelected(item);
}
}
Finally, when your data is finished updating set the visibility to "true" and hide progressbar:
[...] //progress inside your AsyncTask or wherever you have your update
//show Refresh Icon again
refreshIcon.setVisible(true);
//hide the indeterminate progress bar
setSupportProgressBarIndeterminateVisibility(true);
Styling indeterminate progressbar on ActionBar
You'll need to create your own style to apply to the ActionBar. There's a great tutorial on this on the Android Developers Blog. To style the intermediate progress bar, try using indeterminateProgressStyle
and Widget.ProgressBar.Small
. Here's a quick example.
<style name="YourTheme" parent="@android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">@style/ActionBarIPS</item>
</style>
<style name="IndeterminateProgress" parent="@android:style/Widget.ProgressBar">
<item name="android:indeterminateDrawable">@drawable/ic_launcher</item>
</style>
<style name="ActionBarIPS" parent="@android:style/Widget.ActionBar">
<item name="android:indeterminateProgressStyle">@style/IndeterminateProgress</item>
<item name="android:background">@color/white</item>
</style>
ProgressBar in ActionBar
This feature is not supported anymore. If you closely observe the logs you will notice that it is not supported "Progress display unsupported". You can search for more details on the internet posted by Chris Banes a DPE at Google.
How can I place a ProgressBar at the right of the Toolbar?
You can try this. It worked for me. Key here is to define layout_gravity
in the xml: android:layout_gravity="right"
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/material_green_500"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<!-- Color is Brown 500 -->
<ProgressBar
android:id="@+id/toolbar_progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateTint="#795548"
android:indeterminateTintMode="src_in"
android:layout_gravity="right"
/>
</android.support.v7.widget.Toolbar>
Related Topics
Error Java.Lang.Runtimeexception: Stub! in Android with Fitnesse Testing
App and Website Saved Password Is Not Sync
Android/Firebase - Error While Parsing Timestamp in Gcm Event - Null Timestamp
Android Youtubeplayer with Unauthorized Overlay on Top of Player
How to Use Intent for Choosing File Browser to Select File
Android; Geocoder, Why Do I Get "The Service Is Not Available"
Android Internet Connectivity Check Problem
Java.Lang.Noclassdeffounderror: Com.Google.Android.Gms.R$Styleable in Android
How to Hide a Folder in Sdcard in Android
How to Communicate Between Activities
"You Need to Use a Theme.Appcompat Theme (Or Descendant) with the Design Library" Error
Error Non-Default Constructors in Fragments
Android - Onbackpressed() Not Working
Mpandroid Chart Not Displaying Any Labels for Xaxis , What Is Missing