18

I would like to put a progressBar on the action bar but setting

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setProgressBarIndeterminateVisibility(true); 

on onCreate method produces a medium size progressBar (48dip x 48dip) I guess. I want to change the size of the progressBar but I cannot find how to do it. Can you help me please?

7 Answers 7

27

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> 
Sign up to request clarification or add additional context in comments.

7 Comments

I edited my response. It works now. Sorry, I hadn't tested it before. If you want to edit the smaller ProgressBar, use "Widget.ProgressBar.Small" instead. :)
it works even if now I have compatibility problems with actionbarsherlock
In ActionBarSherlock there is this attribute <item name="abIndeterminateProgressStyle">@style/IndeterminateProgress</item>. I am very happy now :) Thanks
If you want to have a progress bar with proper size and padding, check my solution here: stackoverflow.com/questions/15519818/…
It worked for me, but I had to make ActionBarIPS's parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse" and to remove the android:background item in order for it to work with my dark ActionBar.
|
15

Styled ANIMATED indeterminate progressbar icon on ActionBar:

1.Define res/values/styles.xml (This example uses Sherlock library to provide actionbar in old Android versions so, if you arent using Sherlock you should use appropiated parents to define your custom styles):

<?xml version="1.0" encoding="utf-8"?> <resources> <style name="MyTheme" parent="Theme.Sherlock"> <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item> <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item> </style> <style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.Light.ActionBar">   <item name="android:indeterminateProgressStyle">@style/IndeterminateProgress</item> <item name="indeterminateProgressStyle">@style/IndeterminateProgress</item> </style> <style name="IndeterminateProgress" parent="@android:style/Widget.ProgressBar.Small"> <item name="android:indeterminateDrawable">@drawable/progress_indeterminate_custom</item> </style> </resource> 

2.Define res/drawable/progress_indeterminate_custom.xml:

<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item> <rotate android:drawable="@drawable/ic_progress_logo1" android:fillAfter="true" android:fromDegrees="-180" android:pivotX="50%" android:pivotY="50%" android:toDegrees="180" /> </item> <item> <rotate android:drawable="@drawable/ic_progress_logo2" android:fillAfter="true" android:fromDegrees="180" android:pivotX="50%" android:pivotY="50%" android:toDegrees="-180" /> </item> </layer-list> 

3.Above code uses 2 images (ic_progress_logo1 and ic_progress_logo2) as custom progress indeterminated icon. You can use as much images/drawables as you want by adding new item elements. Also, you can apply differents effects/animations. More info here: Animation Resources

4.Apply style you've created using AndroidManifest.xml:

<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:name=".application.MyApplication"> <activity android:name=".activities.MyActivity" android:label="@string/app_name" android:theme="@style/MyTheme" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> 

5.Enjoy your animated custom progress indeterminate icon :D

1 Comment

You may want to provide a complete solution, not just a link. Thanks.
8

I struggled with all of these solutions because I was not using actionbarsherlock. I leveraged off Vlasto Benny Lava's answer, which didn't work for me initially.

Here is what I did step by step:

1) open your styles.xml file, it should be under /res/values/styles.xml. If it doesn't exist create it.

2) paste in:

<resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="myTheme" parent="android:Theme.Holo"> <item name="android:actionBarStyle">@style/ActionBar.MyStyle</item> </style> <style name="ActionBar.MyStyle" parent="@android:style/Widget.Holo.ActionBar"> <item name="android:indeterminateProgressStyle">@style/ActionBarProgressBar.MyStyle</item> </style> <style name="ActionBarProgressBar.MyStyle" parent="@android:style/Widget.Holo.ProgressBar.Small"> <item name="android:minWidth">28dp</item> <item name="android:maxWidth">28dp</item> <item name="android:minHeight">28dp</item> <item name="android:maxHeight">28dp</item> </style> </resources> 

One catchya I came across, you cannot use a theme name of "AppTheme" otherwise the overrides will not work. I used "MyTheme". This theme is using Holo so it has a black ActionBar as seen in image below. If you wish to have a white coloured actionbar then of course replace android:Theme.Holo with android:Theme.Holo.Light etc.

3) Edit AndroidManifest.xml.

your <application> must contain android:theme="@style/myTheme" so that the application is using your override styles from styles.xml

ie, my manifest application tag file looks something like this:

<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/myTheme" android:name="Application Name Here" > 

Because your simply shrinking down the size of the existing progress png there is no need to export any images and move them to your drawable location etc.

Here's what mine looks like, the size of the icon is perfect for me.

enter image description here

Comments

2

For ActionBarSherlock you can change the indeterminate progress by parenting the Widget.ProgressBar.Small, this should work without Sherlock as well.

I fetched the drawables from the android-15 res folder in the SDK, be sure to grab progress_small_white.xml and associated resource.

 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="Dark" parent="Theme.Sherlock"> <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item> <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item> </style> <style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.Light.ActionBar"> <item name="android:indeterminateProgressStyle">@style/IndeterminateProgress</item> <item name="indeterminateProgressStyle">@style/IndeterminateProgress</item> </style> <style name="IndeterminateProgress" parent="@android:style/Widget.ProgressBar.Small"> <item name="android:indeterminateDrawable">@drawable/progress_small_holo</item> </style> </resource> 

2 Comments

works nicely but ofcourse its not moving. how would apply the progress_small_white animation on it?
When you click on the action item, you can replace it with an actual progress bar layout: <FrameLayout xmlns:android="schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center"> <ProgressBar android:layout_width="16dp" android:layout_height="16dp" android:layout_marginLeft="12dp" android:layout_marginRight="12dp" android:layout_gravity="center" style="?android:attr/indeterminateProgressStyle"/> </FrameLayout> refreshItem.setActionView(myProgressBarFrameLaoutInflated);
2

To make the ProgressBar on ActionBar smaller, what you want to do is override Widget.Holo.ProgressBar.Small like this

<style name="Theme.MyStyle" parent="@style/Theme.AppCompat.Light.DarkActionBar"> ... <item name="actionBarStyle">@style/ActionBar.MyStyle</item> ... </style> <style name="ActionBar.MyStyle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid"> ... <item name="android:indeterminateProgressStyle">@style/ActionBarProgressBar.MyStyle</item> ... </style> <style name="ActionBarProgressBar.MyStyle" parent="@android:style/Widget.Holo.ProgressBar.Small"> <item name="android:minWidth">28dp</item> <item name="android:maxWidth">28dp</item> <item name="android:minHeight">28dp</item> <item name="android:maxHeight">28dp</item> </style> 

and set the size to whatever you want.

Comments

1

Paste below code in your on Create method and run this below bar in intermediate mode.
If you want to show on progress percentage, then comment progressBar.setIndeterminate(true); method and uncomment progressBar.setProgress(65) method and maintain your progress

 progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal); progressBar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 24)); //progressBar.setProgress(65); //progressBar.setBackgroundDrawable(getResources().getDrawable(R.color.lock_background_greeen)); progressBar.setIndeterminate(true); // retrieve the top view of our application final FrameLayout decorView = (FrameLayout) getWindow().getDecorView(); decorView.addView(progressBar); ViewTreeObserver observer = progressBar.getViewTreeObserver(); observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { View contentView = decorView.findViewById(android.R.id.content); progressBar.setY(contentView.getY() - 10); ViewTreeObserver observer = progressBar.getViewTreeObserver(); observer.removeGlobalOnLayoutListener(this); } }); 

Comments

0

I have to be compatible with older versions of the Android SDK so I had to keep it simple and used:

Styles.xml

 <style name="TallerTitleBarDialogThemeStyle" parent="@android:style/Theme.Dialog"> <item name="android:windowTitleSize">36dp</item> </style> 

AndroidManifest.xml

android:theme="@style/TallerTitleBarDialogThemeStyle" 

for the activity I need to adjust.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.