Eurig Jones
Eurig Jones

Reputation: 8543

Android ActionBar Progressbar position

I have an ActionBar with a 3 menu items. The last one of these is a refresh button.

The refresh button sets the...

setProgressBarIndeterminateVisibility(true);
setProgressBarVisibility(true);

... to true, which shows the spinning progress icon on the ActionBar. Great!

But the trouble is it shows it in front of all the other icons. I want it to the right of all the icons, so I can hide the refresh button so it looks like it replaces the refresh button when the spinner is visible.

How I can accomplish this?

Upvotes: 3

Views: 2902

Answers (1)

IAmKale
IAmKale

Reputation: 3426

You'll need to make a custom ActionBar layout and position a Progress Bar (set to Indeterminate mode) wherever you want it. Then, instead of calling setProgressBarVisibility(true), just call ProgressBar.setVisibility() based on whether you need to show the spinner or not.

Here's an example of my app's custom ActionBar layout:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/app_name"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"/>

<ProgressBar
    android:id="@+id/action_bar_progress_spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:indeterminate="true"
    android:layout_toStartOf="@id/search_view"
    android:visibility="gone" />

<SearchView
    android:id="@+id/search_view"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:title="@string/menu_search"
    android:icon="@drawable/ic_search"
    android:showAsAction="always"
    android:actionViewClass="android.widget.SearchView"
    android:layout_alignParentLeft="false"
    android:queryHint="@string/menu_search_hint"
    android:iconifiedByDefault="false"
    android:layout_alignParentRight="true"/>
</RelativeLayout>

Just tweak the position of the ProgressBar until it's where you want it. Then, in your Activity's onCreate() set the layout:

ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.activity_start_screen_actionbar);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

Upvotes: 1

Related Questions