As you can see, I have to do TASK1 and continually TASK2. Between 1 and 2, they have delay. And then I want to repeat it infinitely till activity will be finished.
I have no experience for thread.. I can't fix this error.
Help ME!
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
//TASK 1
main_1.setVisibility(View.VISIBLE);
main_2.setVisibility(View.INVISIBLE);
main_1.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_in_left));
Handler mHandler = new Handler(Looper.getMainLooper());
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
//TASK 2
main_1.setVisibility(View.INVISIBLE);
main_2.setVisibility(View.VISIBLE);
main_2.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_in_left));
}
}, 1000);
}
}, 1000, 1000);
And it's my error
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
//TASK 1
YourActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
main_1.setVisibility(View.VISIBLE);
main_2.setVisibility(View.INVISIBLE);
main_1.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_in_left));
}
});
Handler mHandler = new Handler(Looper.getMainLooper());
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
//TASK 2
YourActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
main_1.setVisibility(View.INVISIBLE);
main_2.setVisibility(View.VISIBLE);
main_2.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_in_left));
}
});
}
}, 1000);
}
}, 1000, 1000);
Timer runs on background thread. You can not update ui from background thread, so always use runOnUiThread for that purpose.
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
YourActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
main_1.setVisibility(View.VISIBLE);
main_2.setVisibility(View.INVISIBLE);
main_1.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_in_left));
}
});
Handler mHandler = new Handler(Looper.getMainLooper());
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
//TASK 2
main_1.setVisibility(View.INVISIBLE);
main_2.setVisibility(View.VISIBLE);
main_2.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_in_left));
}
}, 1000);
}
}, 1000, 1000);
Related
The backgrounds of all the pictures are changing at the same time. I have to change one by one, wait 5 seconds after each picture, change the other,
ImageView[] imajlar={img1,img2,img3,img4,img5,img6,img7,img8};
for (final ImageView resmi:imajlar) {
//resmi.startAnimation(fadeout);
new CountDownTimer(16000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
resmi.setBackground(hediye);
resmi.startAnimation(fadein);
onPause();
}
#Override
public void onFinish() {
}
}.start();
Use Handler for this. I hope this helps you.
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
changeImage();
}
}, 5000);
Try Timer with Handler
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
//Your code
}
}, 0, 5000);
Try this code..
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// define your code..
}
},5000);
Do it like
Define an array of images.
String img[]={img1,img2,img3};
and a variable
int i=0;
now in your method
changeImage()
{
if(i<img.length())
{
//Pseudo code(your logic of setting image)
setImage.array[i];
i++;
}
else
{
//This is to start again;
i=0
}
}
Then in you defined handler.
final Handler mHandler=new Handler();
ha.postDelayed(new Runnable() {
#Override
public void run() {
changeImage();
mHandler.postDelayed(this, 5000);
}
}, 5000);
int totalImageSize = yourTotalImageCount;
final Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
changeYourImage(totalImageSize);// this will give the position of image to add/remove
totalImageSize - 1;
if(totalImageSize > 0)
mHandler.postDelayed(this, 5000);
}
}, 5000);
}
I have a timer and timer task and handler and runnable
my code be execute every 10 seconds until ServerResponse variable in not empty and then redirect to another activity.
but when my code redirect to another activity timer task is working !!!!!
how can to stop timer task when we are in another activity??
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
new sendDataToServer().execute();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(serverResponse.length() > 0)
{
Intent intent = new Intent(PayementActivity.this,UserFormActivity.class);
startActivity(intent);
finish();
}
}
}, 10000);
}
});
}
}, 0, 10000);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
new sendDataToServer().execute();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(serverResponse.length() > 0)
{
Intent intent = new Intent(PayementActivity.this,UserFormActivity.class);
if(timer!= null) {
timer.cancel();
timer.purge();
timer= null;
}
startActivity(intent);
finish();
}
}
}, 10000);
}
});
}
}, 0, 10000);
This might not answer your question directly, but just reactive extensions are a nice alternative to using TimerTasks. Check this and this out.
subscription = Observable.interval(10, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())// Runs on a io thread pool
.observeOn(AndroidSchedulers.mainThread()) // Observes on UI thread
.subscribe(timeCount ->{
// Your code here
});
subscription.unsubscribe();// Stops the stream.
I'm trying to change some textview value but when it's not appeared on the screen the value don't change or when it's appeared on screen it changes and when scroll down and scroll back up it's value returns to the old one i tried the following two ways but non of them is working :
final Timer timer=new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
myTextView.setText("sometext");
timer.cancel();
}
});
}
}, 5000, 1000);
and:
MainActivity.this.r1.post(new Runnable() {
public void run() {
myTextView.setText("sometext");
}
});
Try using Handler
private void TestThread() {
Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
handler.post(new Runnable(){
public void run() {
tvTime.setText("SomeText");
}
});
}
}
};
new Thread(runnable).start();
}
Also you can take a look AsyncTask Documentation and onPostExecute() you set the text to TextView.
You have to make sure that the UI is drawn first before you change the value of the TextView , It can be done by adding a layout listener,
ViewTreeObserver vto = getWindow().getDecorView().getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Overrid
public void onGlobalLayout() {
final Timer timer=new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
myTextView.setText("sometext");
timer.cancel();
}
});
}
}, 5000, 1000);
}
}
I would like the Timer to start when I turn ToggleButton ON and to cancel when I turn it OFF. It works, but when I try to start it over again after being cancelled I get an error. Where is the problem? Here is the code:
final ToggleButton btnLive = (ToggleButton)findViewById(R.id.live);
btnLive.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (btnLive.isChecked()) {
timer = new Timer();
timer.schedule(timerTask, 5*1000, 5*1000);
} else {
timer.cancel();
timer.purge();
timer = null;
}
}
});
EDIT: It works with calling timer that way:
timer.schedule(new TimerTask(){
private Runnable runnable = new Runnable() {
public void run() {
new Something().execute();
}
};
public void run() {
handler.post(runnable);
}
}, 5*1000, 5*1000);
Can you explain me why first method is not working? Is it range problem?
Use this type of code and check.
Timer mTimer = new Timer();
mTimer.schedule(new TimerTask()
{
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
}
});
}
}, 5000, 500);
i have a problem with the timerTask in android i have a code like this:
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
countInt = countInt + 1;
textview1.setText(countInt);
}
}, 1000);
every time the timer task get startet my app crashed, i thing because i'm accessing the textview and it is in a other thread right?
how to solve this?
Yes, you are right, it crashes cause' you are accessing views from not an UI thread. To solve this, you can post a Runnable to UI thread using your activity
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
countInt = countInt + 1;
YourActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
textview1.setText(countInt);
}
});
}
}, 1000);
try this..
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
countInt = countInt + 1;
yourActivity.this.runOnUiThread(new Runnable()
public void run(){
{textview1.setText(String.valueOf(countInt))});
}
}
}, 1000);
It crashes because you are messing with something ( textview1.setText(countInt);) that belongs UI thread which is not allowed...