20

I want to show my progressbar in the center of the screen when a processing happens on a button click. But I just want the progressbar without the dialog box..

Is there any way I can do this?

Janusz
  • 176,216
  • 111
  • 293
  • 365
lostInTransit
  • 68,087
  • 58
  • 193
  • 270

5 Answers5

23

It's explained in full in ApiDemos inside the SDK. The example that you want is named: ProgressBar3.java and can be found in \ApiDemos\src\com\example\android\apis\view\

Also, if you want to remove the borders of the dialog so that only the progress bar appears you can define the dialog itself as a transparent view/overlay (it's explained in the examples as well).

reflog
  • 7,368
  • 1
  • 39
  • 46
22

If you need no dialog, just put the progressBar in your layout like any other view item, and set the visibility to 'INVISIBLE' or even 'GONE' by default.

Then just show it whenever you need to by changing it to 'VISIBLE' using setVisibility in your code, and re-hide it when the task is done by making it 'INVISIBLE' or 'GONE' again.

e.g.

MyTask {

// get handle on ProgressBar ViewItem defined in XML (id:progressBar)

ProgressBar progressBar = findViewById(R.id.progressBar);


//starting task, show progress bar

progressBar.setVisibility(View.VISIBLE);


// Do some stuff... 


//task done, hide it again

progressBar.setVisibility(View.GONE);

}

This is a simple version, all done in the UI thread, but this should be easy to adapt if you are using an asynchronous task or thread handlers. Just show, hide, and post any updates to the dialog while you are in the UI thread, and do your long-running task in the background.

zed_0xff
  • 29,646
  • 7
  • 48
  • 70
Jesse Finnerty
  • 221
  • 2
  • 2
  • This work for me, thank you. =D `imgname = (ImageView) v.findViewById(getResources().getIdentifier("idimage", "id", mContext.getPackageName()));` – Florida May 29 '15 at 23:13
3

@reflog: That's not what is needed I guess.

@lostInTransit:

The correct way to achieve this is as follows:

  1. Create ur own dialog
  2. Put progress bar as only view on the dialog
  3. Set dialog window background transparent
  4. Show the dialog.

That's it. It works for me on devices with 1.5 to 2.1. Not tried on 2.2.

-Yogesh

Yogesh
  • 31
  • 1
2

The answer by @Jesse Finnerty solved my issue. what i did was:

private ProgressBar mprogressBar;

The inside oncreate() just like other buttons and textviews i did, mprogressBar = (ProgressBar) findViewById(R.id.progressBar);

Then at the point i want spinning wheel to appear i give command

mprogressBar.setVisibility(View.VISIBLE);

at the point where i want it to disappear .

mprogressBar.setVisibility(View.INVISIBLE);

This is under switch actually. so that as the states in my program change, the progress bar appears and disappears

WBAN
  • 71
  • 1
  • 5
1

There is a great example to do what Yogesh is suggesting at:

How to center progress indicator in ProgressDialog easily (when no title/text passed along)

Credit goes to Macarse for posting it.

Community
  • 1
  • 1