I'm a noob to Android and I am trying to cycle text from an arrayList of strings and display them in an textswitcher. I want the text to change every two seconds. i used this SO question as my guide and have no problem switching the text with a button. However, when I try to cycle the text with a for loop with a 2 second delay it only shows the first text from the arrayList. How can I make the loop continuously execute with a pause? Any help is greatly appreciated.
My Code;
private void updateCounter()
{
try{
for (int i=0; i< CoinShowReader.tickercontent.size(); i++){
mHandler.postDelayed(new Runnable() {
public void run() {
m_switcher.setText((CoinShowReader.tickercontent.get(CoinShowReader.m_counter)));
CoinShowReader.m_counter++;
}
}, 2000);
}
}catch(Exception e){
e.printStackTrace();
}
}
remove the loop, you don't need it, just schedule anther runnable inside the handler like this:
void updateTextView(){
m_switcher.setText((CoinShowReader.tickercontent.get(CoinShowReader.m_counter)));
CoinShowReader.m_counter++;
mHandler.postDelayed(new Runnable() {
public void run() {
updateTextView();
} } ,2000); }
}
that way every call to updateTextView() schedule the next call and so on ...
Note: don't forget to insert trigger to stop that behavior because it's infinity
Related
I want to change from my current activity to another while I am inside a Handler. The idea is other code inside the handler will run until a certain condition doesnt match (with increment of the count value every time). When the count value matches the condition I close the activity and move to another.
my code is:
mHandler = new Handler();
mRunnable = new Runnable() {
#Override
public void run() {
if(count<CONDITION_VALUE)
{
//do other stuff...
count++;
}else
{
//change activity...
finish();
}
mHandler.postDelayed(mRunnable, 4000);
}
};
mHandler.postDelayed(mRunnable, (1000));
The code is running without any error but the old activity is not being destroyed (i guess) and the new activity is reloaded after every 4 seconds.
I want the new activity to load only once.
How can I achieve this?
try this, from enter link description here
in your Activity
#Override
public void onDestroy() {
mHandler.removeCallbacks(mRunnable);
super.onDestroy();
}
I am developing a 2 player board game with button clicks for moves y the 2 players. When playing with the device, I want it to pause before executing performClick() otherwise the move is too quick. I tried
try {
Thread.sleep(1000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
b.performClick();
I have an animation listener before this that sets the status value in a textbox,but that is not getting written in the textbox. Please let me know how to solve this problem.Any help would be greatly appreciated. Thankyou!!
You shouldn't block the main thread. Use a Handler instead:
private final Handler mHandler = new Handler();
private void deferPerformClick() {
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
b.performClick();
}
}, 1000);
}
After pushing a button, i want to close an activity. But, I’d like to wait some seconds before closing it, because users have to read a short message displayed on that activity.
I tried using Thread inside the onClick event
try{
Thread.sleep(2000);
finish();
}
catch(Exception e){}
But, when I push the button, the entire objects are freeze (for example, the button stay pushed).
Then I used a simple Timer
timer.schedule(task(), 2000);
And it seems to work well. Is it correct to use a Timer in this situation, or should I use a Thread or something else?
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Activity.this.finish();
}
}, 2000) ;
The easiest way is probably to use a Handler
private Handler h = new Handler();
...
h.postDelayed(new Runnable() {
#Override
public void run() {
finish();
}
}, 2000);
I want to change the color of a text after every few seconds. I tried generating a random number and using it for setting the color of the text view object in a loop. But the app is not responding. Can anybody help me with this please?
Try running the color-cycling part of your code in a separate thread. For example
Thread thread = new Thread( new Runnable() {
public void run() {
while(true) {
int number = // generate random number ;
runOnUiThread( new Runnable() {
public void run() {
TextView text = // get your TextView ;
text.setTextColor(number);
{
{
try {
Thread.sleep(DELAY);
} catch (InterruptedException e) {}
}
}
}
thread.start();
You will need to run the code that actually changes the TextView via the runOnUiThread because Android does not allow other threads to modify parts of an Activity.
You could use a Handler with .postDelayed() and set it up to have a recursive structure, so each time through it will change the color and then post the next runnable to fire off a few seconds later.
In My application I want to check data after every second, using service.
I have tried to use Timer but it doesn't allow me to use 'runOnUiThread' in service.
In Activity Timer works fine.
or
Is there any other way to trace database at every seconds?
Implement a Runnable, that way you can use the runOnUiThread(runnable) functionality.
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
//do ui update
}
};
Edit: try this: More efficient way of updating UI from Service than intents?
Edit2: http://developer.android.com/resources/articles/timed-ui-updates.html
Use thread u can trace the data base every time
try{
thread=new Thread()
{
#Override
public void run() {
while(set) {
thread_Run();
}
}
};
}
catch(Exception e)
{
Toast.makeText(this, "error", Toast.LENGTH_SHORT).show();
}