Reputation: 1001
I have a TextView
and I would like to update the TextView
every 5 second with each string in my String Array.
Here is the code I tried. It always shows only the last String in the String array.
TextView display;
EditText caption;
Thread thread;
String blinks;
String[] wc;
private CountDownTimer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = (TextView) findViewById(R.id.display);
caption = (EditText) findViewById(R.id.caption);
timer = new CountDownTimer(5000, 20) {
@Override
public void onTick(long millisUntilFinished) {
String[] wc = {"The","Qucik", "Brown","fox","Jumped"};
for (int j = 0; j < wc.length; j++) {
blinks = wc[j];
final String[] titles = {"" + blinks + ""};
for (int i = 0; i < titles.length; i++) {
display.setText(titles[i]);
}
}
}
@Override
public void onFinish() {
try{
yourMethod();
}catch(Exception e){
}
}
}.start();
}
Upvotes: 3
Views: 1231
Reputation: 1191
If the answer is useful. Please do vote :)
final String[] gritArray = getResources().getStringArray(R.array.gritInformation);
//creating new handler allows send and process message
final Handler gritInfoHanlder = new Handler();
//adding runnable to message queque
gritInfoHanlder.post(new Runnable() {
//initializing array position
int tipPosition = 0;
@Override
public void run() {
//set number of tip(randon/another way)
//setting array to textview
gritInfo.setText(gritArray[tipPosition]);
tipPosition++;
//if array exist its length remove callback
if (tipPosition == gritArray.length) {
gritInfoHanlder.removeCallbacks(this);
//delayed handler 10 sec
} else {
gritInfoHanlder.postDelayed(this, 1000 * 10);
}
}
});
Upvotes: 0
Reputation: 3562
You need to make sure that you call TextView.setText()
only once per tick. Then you can go with your approach (CountDownTimer
) or with Antrrommet's (Handler
). With your approach, this will look like something like:
int counter = 0; // field member, NOT local variable
@Override
public void onTick(long millisUntilFinished) {
display.setText("{" + wc[counter++] + "}");
if (counter == 5) counter = 0;
}
Upvotes: 1
Reputation: 15414
final String[] wc = {"The", "Qucik", "Brown", "fox", "Jumped"};
final android.os.Handler handler = new android.os.Handler();
handler.post(new Runnable() {
int i = 0;
@Override
public void run() {
display.setText(wc[i]);
i++;
if (i == wc.length) {
handler.removeCallbacks(this);
} else {
//5 sec
handler.postDelayed(this, 1000 * 5);
}
}
});
Upvotes: 3
Reputation:
try to play Around with handler in android , i think you are asing something like this :
String[] wc = {"The","Qucik", "Brown","fox","Jumped"};
int j = 0;
Handler handler = new Handler();
Runnable runit = new Runnable() {
@Override
public void run() {
try{
for (; j < wc.length; j++) {
blinks = wc[j];
final String[] titles = {"" + blinks + ""};
for (int i = 0; i < titles.length; i++) {
display.setText(titles[i]);
}
handler.postDelayed(this,5000);
}
}
catch (Exception e) {
// handle exception
}
}
};
handler.postDelayed(runit, 5000);
Upvotes: 1