I want to print a single word at a time in textView and then using sleep and then next word.
but it int'n working.. need help.
String s = MainActivity.check;// String check defined in mainactivity this is second
String[] words = s.split(" ");
EditText et = (EditText) findViewById(R.id.editText2);
for(int i=0;i<words.length; i++ ){
et.setText(words[i]);
Thread.sleep(1000);
}
above output only last word in textView
You are setting th text new on each iteration. Try et.setText(et.getText() + words[i]);
This takes the text that is already in the TextView then appends the new word.
Any updates to the UI in an Android application must happen in the UI thread. If you spawn a thread to do work in the background you must marshal the results back to the UI thread before you touch a View.
You must use Handler class to do so...
public EditText et;
Handler handler;
oncreate(..) {
setContentView(R.layout.main);
et = new EditText(context);
Thread t =new Thread(){
public void run() {
handler.post(new Runnable() {
public void run() {
et.setText("test");
}
});
}
}};
t.start();
}
Try using handler instead:
for(int i=0;i<words.length; i++ ){
final int j = i;
String text = words[j];
new Handler().postDelayed(new Runnable() {
public void run() {
et.setText(text);
}
}, 1000);
}
ok , Here is your answer :
int i = 0;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
textView.setText(""+i);
i++;
handler.postDelayed(this, 1000);
}
}, 1000);
Related
I want to automatically generate random numbers every second when the button is pressed only once in android
final Handler handler = new Handler();
final Random random = new Random();
final Runnable task = new Runnable() {
#Override
public void run() {
int randomNumber = random.nextInt();
handler.postDelayed(this, 1000);
}
};
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
task.run();
}
});
Create a runnable and a handler to activate that runnable every second, once the button is clicked.
Runnable r = new Runnable() {
#Override
public void run() {
randomNumber();
}
}
then create the handler for that runnable:
Handler handler = new Handler();
inside your onClickListener for the button trigger the runnable by calling the following:
handler.postDelayed(r, 1000);
EDIT:
your random number generator could be something like this:
public int randomNumber() {
Random random = new Random();
int randomNumber = random.nextInt(100);
return randomNumber;
}
it gives you a random number between 0-100
I want to print the current second using a handler. I record a video for exactly 10 seconds and want to set the text of a TextView every second.
Recording 10 seconds works like that:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
stopRecordingVideo();
}
}, 11000); // don't know why 11000 but it only works this way
After the 10 seconds the method stopRecordingVideo() gets executed. So how can I change the text every second of the TextView?
Working answer:
int t = 0;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
t++;
textView.setText(getString(R.string.formatted_time, t));
if(t<10) {
handler.postDelayed(this, 1000);
}
}
}, 1000);
Where formatted_time is something like that:
<string android:name="formatted_time">%d seconds</string>
To print text every second, you can use CountDownTimer. But if you want to achieve this with try below code:
void startTime(){
//Post handler after 1 second.
handler.postDelayed(runnable, 1000);
}
int totalDelay=0;
Handler handler = new Handler();
Runnable runnable = new Runnable() {
#Override
public void run() {
totalDelay++;
if(totalDelay<=10){
//If total time is less then 10 second execute handler again after 1 second
handler.postDelayed(runnable, 1000);
}
textView.setText(totalDelay+" Second");
}
};
Try this, basically do the increment in a worker thread, but updating the text view is done by main's thread handler.
Thread worker= new Thread(new Runnable() {
#Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
// stop recording after 10 seconds
if (i == 9) {
handler.post(new Runnable() {
#Override
public void run() {
stopRecordingVideo();
}
});
}
else{
// set text for each second
handler.post(new Runnable() {
#Override
public void run() {
textView.setText(String.valueOf(i+1));
}
});
}
}//ends for()
worker.start()
I want to show custom dialogs in order. Each dialog must shown for 5 second then it must be dismissed and the other one must be shown. I use for loop and my code looks like:
for(int i = 0 ; i < 10 ; i++){
popupView.show();
SystemClock.sleep(3000);
popupView.dismiss();
SystemClock.sleep(1000);
}
Try this code and adjust the time interval as your need.
showDialogs();
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doShowDialog = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
alertDialog.dismiss();
if (mDialogCounter < 5) {
showDialogs();
mDialogCounter++;
}
}
});
}
};
timer.schedule(doShowDialog, 5000, 5000);
Hi I've been looking around and have got the following code.
It sort of works, onCreate the total TextField updates to the value it's meant to however after that nothing..
The onCreate bit
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
#Override
public void run() {
handler.post(new Runnable(){
public void run() {
total.setText(totalTimeToString());
}
});
}
};
new Thread(runnable).start();
The totalTimeToString()
public String totalTimeToString(){
String temp = convert(total());
return temp;
}
the total() is simply getting the value of two number pickers and adding them together and the convert() method converts the total to HH:MM:SS string format.
Any help would be welcome. I am a complete newbie.
I have a simple program with one button. Once I clicked on the button loop started and should display the current value in TextView dynamically. But this display the value when loop is completed.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView) findViewById(R.id.textView2);
}
public void btnClick(View v) throws Exception {
for (int i=0;i<10;i++){
tv.setText("Value of I "+i);
Thread.sleep(500);
}
}
My expected output is displaying values from 0, 1,.... dynamically but displayed 9 at last. if I use append I have to wait until loop terminated. Please help me.
Try this:
int i = 0; //declare this globally
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(i != 10) {
text.append(" " + i);
i++;
handler.postDelayed(this, 1000);
}
}
}, 1000);
}
Make sure you declare int i = 0 globally
tv.append("text here") is your answer.
don't use Thread.sleep() in your main UI thread this should give an exception instead use a new Thread like this
Thread myThread = new Thread(new Runnable() {
#Override
public void run() {
for (int i=0;i<10;i++){
tv.setText("Value of I "+i);
Thread.sleep(500);
}
}
});
myThread.start();
or if there is more complex operations you make you will have to use AsyncTask calss