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);
}
}
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);
}
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);
This is my code on Activity to Invalidate the canvas it is not invalidating. Means onDraw() is not getting called even once;
public GraphView view;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view = GraphView(this,null);
runplotTimer();
}
public void runplotTimer()
{
Timer t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
InvalidateTimer();
}
},1000,40);
}
public void InvalidateTimer()
{
this.runOnUiThread(new Runnable() {
#Override
public void run()
{
//Log.d(ALARM_SERVICE, "Timer of 40 miliseconds");
view.InvalidateGraph();
}
});
}
on View class this is method which is gettting called from Activity. other OnDraw declaration is same as required.
public void InvalidateGraph()
{
m_bCalledPlotRealTimeGraph = true;
invalidate(chanX_count1, 0, chanX_count1+7, graphheight);
}
Any help please ?
You are attempting to make changes to the View on a Timer Thread, which will not work. You need to call invalidate on the main (UI) thread:
((Activity) view.getContext()).runOnUiThread(new Runnable() {
#Override
public void run() {
invalidate(chanX_count1, 0, chanX_count1+7, graphheight);
}
});
you need to Start the Timer
Timer t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
InvalidateTimer();
}
},1000,40);
t.start()
Instead of Timer use Handler.
class UpdateHandler implements Runnable {
#Override
public void run(){
handler.sendEmptyMessageAtTime(0, 1000);
handler.postDelayed(this, 1000);
}
}
private Handler handler = new Handler(Looper.getMainLooper()) {
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//Call your draw method
}
}
};
Inside onCreate and onResule write
if( mupdateTask == null )
mupdateTask = new UpdateHandler();
handler.removeCallbacks(mupdateTask);
Call your handler using
handler.postDelayed(mupdateTask, 100);
I am new to android development. I am making an app based on dictation.
In the activity below I have 15 small audio clips; the files play one after the other with a delay of 5 seconds.
My question is how can I play only 10 clips from the 15; also I want the 10 clips to be randomly played.
Any help will be appreciated.
package com.example.dictationary;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
public class Level1 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level1);
MediaPlayer n1 = MediaPlayer.create(this,R.raw.assassin);
final MediaPlayer n2 = MediaPlayer.create(this,R.raw.accessible);
final MediaPlayer n3 = MediaPlayer.create(this,R.raw.bashfully);
final MediaPlayer n4 = MediaPlayer.create(this,R.raw.blistering);
final MediaPlayer n5 = MediaPlayer.create(this,R.raw.butter);
final MediaPlayer n6 = MediaPlayer.create(this,R.raw.campaign);
final MediaPlayer n7 = MediaPlayer.create(this,R.raw.circumstances);
final MediaPlayer n8 = MediaPlayer.create(this,R.raw.clinching);
final MediaPlayer n9 = MediaPlayer.create(this,R.raw.deferential);
final MediaPlayer n10 = MediaPlayer.create(this,R.raw.distinguished);
final MediaPlayer n11 = MediaPlayer.create(this,R.raw.embarrass);
final MediaPlayer n12 = MediaPlayer.create(this,R.raw.etiquette);
final MediaPlayer n13 = MediaPlayer.create(this,R.raw.fatigue);
final MediaPlayer n14 = MediaPlayer.create(this,R.raw.feasible);
final MediaPlayer n15 = MediaPlayer.create(this,R.raw.glitch);
n1.start();
Timer buttonTimer = new Timer();
buttonTimer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
n2.start();
}
});
}
}, 5000);
Timer buttonTimer1 = new Timer();
buttonTimer1.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
n3.start();
}
});
}
}, 10000);
Timer buttonTimer2 = new Timer();
buttonTimer2.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
n4.start();
}
});
}
}, 15000);
Timer buttonTimer3 = new Timer();
buttonTimer3.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
n5.start();
}
});
}
}, 20000);
Timer buttonTimer4 = new Timer();
buttonTimer4.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
n6.start();
}
});
}
}, 25000);
Timer buttonTimer5 = new Timer();
buttonTimer5.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
n7.start();
}
});
}
}, 30000);
Timer buttonTimer6 = new Timer();
buttonTimer6.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
n8.start();
}
});
}
}, 35000);
Timer buttonTimer7 = new Timer();
buttonTimer7.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
n9.start();
}
});
}
}, 40000);
Timer buttonTimer8 = new Timer();
buttonTimer8.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
n10.start();
}
});
}
}, 45000);
Timer buttonTimer9 = new Timer();
buttonTimer9.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
n11.start();
}
});
}
}, 50000);
Timer buttonTimer10 = new Timer();
buttonTimer10.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
n12.start();
}
});
}
}, 55000);
Timer buttonTimer11 = new Timer();
buttonTimer11.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
n13.start();
}
});
}
}, 60000);
Timer buttonTimer12 = new Timer();
buttonTimer12.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
n14.start();
}
});
}
}, 65000);
Timer buttonTimer13 = new Timer();
buttonTimer13.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
n15.start();
}
});
}
}, 70000);
}
}
You can put them in an array or list and access them with Random. Example:
Creating an ArrayList:
//Here we create the ArrayList
LinkedList<MediaPlayer> list = new LinkedList<MediaPlayer>(15);
list.add(new MediaPlayer .... // for each element.
Acess this sperated
// now we create the random int
Random r = new Random();
int pointer = r.nextInt(list.size());
// and here we access the "random" MediaPlayer
list.remove(pointer).start(); // gets a random media
where nextInt returns a value between 0 and <=list.size
// edit changed code
You cannot with the way your code is now organized...
You should refactor to
`MediaPlayer n = new MediaPlayer[];
n[1] = MediaPlayer.create(this,R.raw.assassin);
//...
n[15] = MediaPlayer.create(this,R.raw.assassin);
and then just pick a random number in interval [0-15) (15 excluded) and play that one.
Furthermore, your code would simplify alot, because only one recurring TimerTask is needed, and every time it would invoke a random number (word). However, this would introduce repeated words - to ensure no word is repeated, add the randomly picked number to a alreadyHeard list, and instead of playing that index simply pick a new one and test it again (in fact, while() pick them until you come up with one that isn't in the alreadyHeard list)
That should get you what you want :)
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);