Progressbar Under Action Bar

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.

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);

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>

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>

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.



Related Topics



Leave a reply



Submit