Reputation: 49
Before asking the question let me tell you that I am new in android development. My question is regarding Threads and wait(); function. Right now I am trying to implement the thread which should change the color of text every 3 seconds but the program crashes as soon as I start it. Here is the code.
package care.client_application;
public class MainActivity extends AppCompatActivity {
Thread Thread3=null; //Coloring thread
public TextView Warning;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Warning= (TextView) findViewById(R.id.warning);
UIHandler = new Handler(); //Initialization of handler
this.Thread3= new Thread(new Thread3());
this.Thread3.start();
}
class Thread3 implements Runnable{
@Override
public void run() {
Warning.setTextColor(Color.rgb(200,200,0));
wait(3000);
Warning.setTextColor(Color.rgb(200,0,0));
}
}
}
Upvotes: 1
Views: 271
Reputation: 4513
public class MainActivity extends AppCompatActivity {
private TextView warningText;
private final long TIME_TO_CHANGE = 3000;
private Handler handler = new Handler();
private int counter = 0;
private int[] R = {0, 200}, G = {0, 200}, B = {200, 0};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
warningText = (TextView) findViewById(R.id.warning);
warningText.setTextColor(Color.rgb(0,0,0));
handler.postDelayed(new Runnable() {
@Override
public void run() {
updateTextColor();
handler.postDelayed(this, TIME_TO_CHANGE);
}
}, TIME_TO_CHANGE);
}
private void updateTextColor(){
runOnUiThread(new Runnable() {
@Override
public void run() {
counter = (counter+1)%2;
warningText.setTextColor(Color.rgb(R[counter],G[counter],B[counter]));
}
});
}
@Override
public void onPause(){
super.onPause();
handler.removeCallbacksAndRunnables(null);
}
}
Use Handlers for tasks that require waiting. Thread.sleep() is not recommended in Android.
Upvotes: 1
Reputation: 3189
First, You shouldn't use that it freezes ui and thats not a good approach. Second if you have to do this then you have to use runonuithread as to change ui on run time you have to do this like:
class Thread3 implements Runnable{
@Override
public void run() {
getActivity().runOnUiThread(new Runnable()
{
@Override
public void run()
{
Warning.setTextColor(Color.rgb(200,200,0));
}
});
wait(3000);
getActivity().runOnUiThread(new Runnable()
{
@Override
public void run()
{
Warning.setTextColor(Color.rgb(200,0,0));
}
});
}
I hope it helps you do let me know if it works. Best of luck.
Upvotes: 0
Reputation: 23881
I want to change the color of text every 3 seconds
Why not use a Handler
instead of Thread
private Runnable task = new Runnable () {
public void run() {
//update textview color
Warning.setTextColor(Color.rgb(200,200,0));
mHandler.postDelayed(task, 3000); //repeat task
}
};
Call it using:
private Handler mHandler = new Handler();
// call updateTask after 3 seconds
mHandler.postDelayed(task, 3000);
To stop the task:
@Override
protected void onPause() {
mHandler.removeCallbacks(task);
super.onPause();
}
Upvotes: 1
Reputation: 603
First of all you can't update any UI component on Run method of thread like your doing
Warning.setTextColor(Color.rgb(200,200,0)); // this wrong way to do this
Use Handler to perform any UI update
Upvotes: 0