How to use Rotating Progress Bar? - java

Hi i'm using Rotating Progress Bar in my Android Music Plyer Application....I'm not able to stop it. While working with horizontal Progress bar i used handler to stop and start it.
But while working with Rotating One, The progress bar goes into Infinite Loop.....
Can you please suggest method to stop the indefinite loop. Thanks in advance.

How about using ProgressBar#dismiss() method?
EDIT: dismiss() is only for ProgressDialog. For ProgressBar you should toggle the Visibilty of the View.
If mHandler is a Handler bound to your UI thread and mProgress is your ProgressBar, you can have something like the following from inside the run method of your background thread:
mHandler.post(new Runnable() {
public void run() {
mProgress.setVisibility(View.INVISIBLE);
}
});

You can dismiss a ProgressDialog. A progressBar is just a view you can make set its visibility as visible or invisible based on your requirement

Drawable d = yourActivity.this.getResources().getDrawable(android.R.drawable.ic_dialog_alert);
d.setBounds(progressbar.getIndeterminateDrawable().getBounds());
progressbar.setIndeterminateDrawable(d);

Related

How to dismiss Vaadin 8 confirmation dialog while performing lengthy operation

I have Vaadin 8 confirmation code there it performs a long lasting task. I would like to dismiss the dialog and show a progress bar:-
ConfirmDialog.show(UI.getCurrent(), "Dynamic Report warning caption",
"More than " +rows+ " rows in this table. It will take long time to generate this report.\nDo you still want to continue? ",
SalkkuTM.getI18N("PortfolioManagementMenuBar.deleteData.confirmDialog.yes.caption"),
SalkkuTM.getI18N("PortfolioManagementMenuBar.deleteData.confirmDialog.no.caption"), new ConfirmDialog.Listener() {
public void onClose(ConfirmDialog dialog) {
ProgressBar bar = new ProgressBar();
bar.setIndeterminate(true);
if (dialog.isConfirmed()) {
layout.addComponent(bar);
// this method does a long lasting task.
dynamicReportParameterGenerator();
bar.setVisible(false);
}
}
});
I would like to dismiss this dialog as soon as user select yes. And I would like to show an Indeterminate progress bar. I couldn't manage it. How to do it? Please let me know how can I do it?
This is FAQ stuff, you need to use Server Push and update progress bar in background thread. There is a blog post and video about it here
https://vaadin.com/blog/community-answer-processing-a-file-in-a-background-thread
There is a good discussion about the topic here too
Update Vaadin Progressbar with push asynchronously
And I would like to show an Indeterminate progress bar.
This means that you can simplify things a little. If you do not want to update progress bar during the operation, you just need to put progress bar in UI in Indeterminate mode and once complete, update the UI again.

Start and then pause animation in android

I have a circular layout and there are "n" buttons in this layout. I start the animation on that layout when the activity starts.
When I click on any button the animation should stop and a dialog appear with the message, "You have clicked this 'XYZ' button".
The code I am using:
animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
animation.setFillEnabled(true);
animation.setFillAfter(true);
findViewById(R.id.circle_layout).startAnimation(animation);
and the animation XML:
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="15000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:toDegrees="360" >
There is no pause in animation in android. I checked many questions relating to the same on StackOverflow but no luck. You can still give a try to pausing the Activity itself using this link which may help.
The link states the following:
Pause Your Activity
When the system calls onPause() for your activity, it technically means your activity is still partially visible, but most often is an indication that the user is leaving the activity and it will soon enter the Stopped state. You should usually use the onPause() callback to:
Stop animations or other ongoing actions that could consume CPU.
Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email).
Release system resources, such as broadcast receivers, handles to sensors (like GPS), or any resources that may affect battery life while your activity is paused and the user does not need them.
There is no pause as posted. But you can mimic it.
This answer worked for me:
How do I pause frame animation using AnimationDrawable?
public class PausableAlphaAnimation extends AlphaAnimation {
private long mElapsedAtPause=0;
private boolean mPaused=false;
public PausableAlphaAnimation(float fromAlpha, float toAlpha) {
super(fromAlpha, toAlpha);
}
#Override
public boolean getTransformation(long currentTime, Transformation outTransformation) {
if(mPaused && mElapsedAtPause==0) {
mElapsedAtPause=currentTime-getStartTime();
}
if(mPaused)
setStartTime(currentTime-mElapsedAtPause);
return super.getTransformation(currentTime, outTransformation);
}
public void pause() {
mElapsedAtPause=0;
mPaused=true;
}
public void resume() {
mPaused=false;
}
}
Please note: this does not technically 'pause' the animation because it keeps continuously calling the transformation. But can keeps a persistent transformation that 'mimics' the same functionality.
I tried this with a RotateAnimation and worked just fine. But it will not lower the CPU/framerate when it is 'paused' as it does when you cancel the animation.
There is no great way to pause an animation mid-cycle.
You could subclass RotateAnimation and intercept the currentTime value in getTransformation and feed it the same time while you want your animation to be paused.
If you can afford to only support HC+, then you should look into using property animations instead of View animations.

How can I test the result of a button click that changes the Activity's view asynchronously?

I am trying to write some Activity tests for an app, and one particular scenario that I want to test is that when I click a certain button, the Activity view updates accordingly. However, clicking the button causes a somewhat long running asynchronous task to start and only after that task is completed does the view change.
How can I test this? I'm currently trying to use the ActivityInstrumentationTestCase2 class to accomplish this, but am having trouble figuring out how to have the test 'wait' until the asynchronous part of the button click task is complete and the view updates.
The most common and simplest solution is to use Thread.sleep():
public void testFoo() {
TextView textView = (TextView) myActivity.findViewById(com.company.app.R.id.text);
assertEquals("text should be empty", "", textView.getText());
// simulate a button click, which start an AsyncTask and update TextView when done.
final Button button = (Button) myActivity.findViewById(com.company.app.R.id.refresh);
myActivity.runOnUiThread(new Runnable() {
public void run() {
button.performClick();
}
});
// assume AsyncTask will be finished in 6 seconds.
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
e.printStackTrace();
}
assertEquals("text should be refreshed", "refreshed", textView.getText());
}
Hope this helps.
If you're using Eclipse, you could use the debugger by setting a breakpoint in the code that updates the view. You could also set some breakpoints in the long running task to watch and ensure that all your code is executing.
An alternative, write some log or console outputs in your long-running task and the view updater code, so you can see the progress without interrupting the thread by a debugger.
As a piece of advise, if its a long-running process, you should be showing a progress bar of some description to the user, so they aren't stuck there thinking "Is something happening?". If you use a progress bar with a maximum value, you can update it in your long-running task as it is running, so the user can see the activity going from 10% to 20%... etc.
Sorry if you were expecting some kind of jUnit-specific answer.
I ended up solving this by using the Robotium library's Solo.waitForText method that takes a string and timeout period and blocks until either the expected text appears or the timeout occurs. Great UI testing library.

Change an image for a second

ImageView image = (ImageView) findViewById(R.id.imageview);
image.setImageResource(drawable.image1);
SystemClock.sleep(1000);
image.setImageResource(drawable.image2);
I am trying to change the image for a second, my code above doesn't work but not sure why?
Should I be using a thread? or does anyone have any better ideas?
EDIT
To clarify on the problem:
The image being displayed as "drawable.image2"
I want "drawable.image1" to be shown for one second then change to "drawable.image2".
EDIT2:
This code is used in the onClick. When a user clicks the image it needs to change for one second
I'd recommend using a TimerTask with a Timer. You can set it up like this:
protected void showDelayedImages() {
mImageView.setImageResource(resId1);
Timer timer = new Timer();
timer.schedule( new MyTimerTask(), 1000 );
}
private class MyTimerTask extends TimerTask {
#Override
public void run() {
runOnUiThread( new Runnable() {
#Override
public void run() {
mImageView.setImageResource(resId2);
}
} );
}
}
Thread.sleep(1000);
should do it. Although there are better ways too.
Use debug mode and set breakpoints on each call to setImageResource. Step through and see if each is getting called to see if your image is changing properly.
In real world cases, you probably want to change the image based on some user action, or for example change an icon while a thread is processing, then change it bad when complete. For this example check out AsyncTask.
use R.drawable.image1 instead of drawable.image1
It looks like you're performing the "switch" in an onCreate() method, the sleep will probably just make your Activity load slower since at this stage there isn't actually anything written to the page.
To have your image change you need to perform the switch on the UI thread and you need to perform it after the image has been inflated and added to the page.
Try adding this code in an "onClick" event.

Android: Showing indeterminate progress bar in TabHost activity

I know that the following code should show and hide a tiny circular progress bar with the following code in Android:
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setProgressBarIndeterminateVisibility(true);
setProgressBarIndeterminateVisibility(false);
The problem is that I am using a TabHost and I need to be able to do this from one of the "child" activities. Is there any way I can go about doing this?
I found this question on the intarwebs but as you can see, it went unanswered.
And I found the answer. In your parent activity, before you do anything, you need to do the requestWindowFeature call, and then in your child activity you call getParent().setProgressBarIndeterminateVisibility(true/false);
Just for completeness:
If the task is running in a different thread other than Main ui thread, you can do:
this.runOnUiThread(new Runnable() {
public void run() {
getParent().setProgressBarIndeterminateVisibility(mToggleIndeterminate);
}
});

Categories

Resources