Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ProgressDialog in ActionBar (Android)

I am trying to move a ProgressDialog to my ActionBar.

I know how to put it in the action bar and animate it (I think), like this:

In progress.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1" >

    <ProgressBar
        android:id="@+id/progress"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ProgressBar>

</LinearLayout>

in menu.xml

<item
    android:id="@+id/Refresh"
    android:icon="@drawable/ic_menu_refresh"
    android:showAsAction="always"
    android:title="Refresh"/>

in my activity

    case R.id.Refresh:
        item.setActionView(R.layout.progress);
        return true;

Here is my current PD (in the activity) in onPreExecute and onPostExecute of my AsyncTask:

// Pre  
dialog = new MyProgressDialog(---.this);
dialog.show(---.this);


// Post    
dialog.dismiss(---.this);

So how do I move this so that the action bar indicator is activated in onPreExecute and then stopped in onPostExecute?

EDIT: I am not only looking for a refresh, but a "loading" indicator when you first load an activity (or do something that requires the PD to activate). It should be hidden by default.

like image 239
TheLettuceMaster Avatar asked Sep 30 '12 15:09

TheLettuceMaster


People also ask

Is ProgressDialog deprecated?

ProgressDialog 's look can be replicated by placing a ProgressBar into an AlertDialog . You can still use it, but Android does not want you to use it, that is why it is deprecated.

How do I change font size in progress dialog android?

You can use a spannable string and set the color and size for the spannable string. Use the spannable string to set text color and size.

How can I add progress bar in android?

In android there is a class called ProgressDialog that allows you to create progress bar. In order to do this, you need to instantiate an object of this class. Its syntax is. ProgressDialog progress = new ProgressDialog(this);


2 Answers

Guess I will have to answer my own question. This was very simple actually.

in Activity, just above setContentView

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

AsyncTask

// pre
setProgressBarIndeterminateVisibility(true);

// Get Data

// post
setProgressBarIndeterminateVisibility(false);
like image 162
TheLettuceMaster Avatar answered Oct 06 '22 19:10

TheLettuceMaster


if you have to require in onCreate()

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

or if you are using the support package it would be

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

if you finding any trouble then make comment.

like image 31
Harshid Avatar answered Oct 06 '22 21:10

Harshid