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.
Related
I am updating the text user types in a textview to database every 5 seconds.
The handler collects data from textview and save it to database.
But the update code runs even after I press back button and return to MainActivity.
How can I stop running the code after exiting from activity.
public class CreateBoxActivity extends AppCompatActivity {
Long current_BoxID = null, parent_BoxID = null;
Handler h=new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_box);
h.post(new Runnable(){ //Run this every five seconds to update the data
#Override
public void run() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String editeddate = sdf.format(Calendar.getInstance().getTime());
String titleText = ((EditText)findViewById(R.id.new_box_title)).getText().toString();
String descriText = ((EditText)findViewById(R.id.new_box_descri)).getText().toString();
Boxes nbx = new Boxes(1, null , titleText, descriText, editeddate, editeddate);
BoxesController.UpdateBox(nbx);
h.postDelayed(this,5000);
}
});
}
You need to release the handler resource when the activity is destroyed.
#override
protected void onDestroy() {
super.onDestroy();
h.removeCallbacksAndMessages(null);
h = null;
}
Create runnable outside of the method like :
Runnable runnable =new Runnable() {
#Override public void run() {
}
};
On your activity's onPause call:
h.removeCallback(runnable);
You can remove the handler in onPause
Runnable runnable =new Runnable() {
#Override public void run() {
}
};
new Handler().removeCallbacks(runnable);
I want to refresh an activity which will change a textview value after every 5 second without any animation.i searched in stackoverflow also but can't get proper solution.please help
Im not sure if this answers your question but would it work if set the text in the textView every 5 seconds? Using the txtView.setText("your text"); and then have a delay in between each time you set it? I hope this works and makes sense
You can do it with a Handler. Here's how :
private boolean started = false;
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
runOnUiThread(new Runnable() {
#Override
public void run() {
textview.setText("####"); // <- text to be updated every 5 sec
if(started) {
start();
}
}
});
}
};
public void stop() {
started = false;
handler.removeCallbacks(runnable);
}
public void start() {
started = true;
handler.postDelayed(runnable, 2000);
}
I've tried using Thread, but it was unsuccessful, the textView didn't changed after I change the TextView text using EditText as input. Help me, please!
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
//shared is SharedPreferences object that I define as an instance variable
String inp = shared.getString("input", def);
textView.setText(inp);
Log.d("input",inp);
});
thread.start();
try this:
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
//shared is SharedPreferences object that I define as an instance variable
String inp = shared.getString("input", def);
runOnUiThread(new Runnable() {
#Override
public void run() {
textView.setText(inp);
Log.d("input",inp);
}
});
});
thread.start();
for updating UI you should use the main ui thread. take a look at :
Android runOnUiThread explanation
Why are you doing it in a separate thread. Even if you want to, you cannot update the any UI component such as textView in a non UI thread.
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);
I am working on a Testing Project for me , just so I could learn more , so now I need to update the text of a TextView constantly every 250 milliseconds through a for(;;) loop , it happens after a Button click ... My problem is that whenever I press that button my app freezes (Yes my button is totally working , verified through previous testings) , I am using a handler to the Main thread doesn't get affected while the Runnable is up ... Here is my code of the Button and Handler ...
final Handler handler = new Handler();
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handler.post(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
for(;;){
String a = Shells.sendSingleCommand("free");//Here I send a command "free" and it returns its output
text.setText(a);//text is my TextView which is used through my experimentations ...
synchronized(this){
try{
wait(250);
}catch(Exception e){
}
}
}
}
});
}
});
If you need anymore info ask please :)
use handler.postDelayed for updating textview constantly every 250 milliseconds instead of using for loop to avoid freeze current Activity as :
Handler handler=new Handler();
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handler.post(runnable);
}
});
Runnable runnable=new Runnable(){
#Override
public void run() {
String a = Shells.sendSingleCommand("free");
text.setText(a);
handler.postDelayed(runnable, 250);
}
};
Android doesnt allow you to do long tasks in the main thread. If you need to do something like this I recommend moving the for loop and depdendent code into a separate thread..