Update two TextViews inside a runnable - android

Can I update two textviews inside a runnable?
Because my code can only update one textview.
I got this method that updates 2 textviews that contains the address and date from the EXIF data of a photo.
public void launchRingDialog() {
final ProgressDialog ringProgressDialog = ProgressDialog.show(ReportIncident.this, "Please wait ...", "Rendering Image EXIF Data ...", true);
ringProgressDialog.setCancelable(false);
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(5000);
loadExifData();
r.setDate(mExif.getAttribute(ExifInterface.TAG_DATETIME));
tvLocation.setText(getaddress());
tvTime.setText(r.getStringDate());
r.setLati(Double.parseDouble(mExif.getAttribute(ExifInterface.TAG_GPS_LATITUDE)));
r.setLongi(Double.parseDouble(mExif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE)));
} catch (Exception e) {
}
ringProgressDialog.dismiss();
}
}).start();
}

You cannot update the UI in a different Thread than the UI Thread in Android. To do that, a simple way you can use is:
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable(){
tvLocation.setText(getaddress());
tvTime.setText(r.getStringDate());
});

Related

Get counter value from server and update in Textview

I have a server giving me live data in JSON format which updates every second. I have to display that in my android app.
I am a beginner and I tried Async Task updating every second via a thread and setting a delay on it.
Thread t = new Thread() {
#Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
#Override
public void run() {
// Perform the HTTP request for data and process the response.
counterAsyncTask task=new counterAsyncTask();
task.execute(REQUEST_URL);
}
});
}
} catch (InterruptedException e) {
}
}
};
t.start();
It runs out of memory and crashes after some time
Are there any alternates?
Try putting your code into handler thread
Runnable runnable = new Runnable() {
#Override
public void run() {
mHandler.postDelayed(this, 1000);
counterAsyncTask task=new counterAsyncTask();
task.execute(REQUEST_URL);
}
};
// start it with:
mHandler.post(runnable);

Set ProgressBar in Cursor while SQLite

How I can set dynamically set the progressbar status after reading a value from db SQLite?
I have this code.
int i = 0;
while (!c.isAfterLast()) {
i++;
pb.setProgress(i)
}
But my problem is that progress bar is update only at finish while so without "liveEffect"
You can use runOnUiThread method of Activity class:
runOnUiThread(new Runnable() {
#Override
public void run() {
pb.setProgress(i);
}
});
More here: https://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)
Actually, execution time is too low that by live effect not appear . take large cursor around 10000 value then apply loop now you can see progress
I resolved my problem with Handler and Thread.sleep (for simulate live)
new Thread(new Runnable() {
public void run() {
do {
mProgressStatus++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mHandler.post(new Runnable() {
public void run() {
pb.setProgress(mProgressStatus);
}
);
} while (c.moveToNext());
}
}).start();

why can I touch UI in non-uithread by looper?

thread = new Thread() {
public void run() {
super.run();
System.out.println("run:" + Thread.currentThread().getName());
Looper.prepare();
handler = new Handler();
Looper.loop();
};
};
thread.start();
And then
handler.post(new Runnable() {
#Override
public void run() {
Toast.makeText(MActivity.this,Thread.currentThread().getName(),0).show();
}
});
the code run correct.
but the toast shows :"Thread-217"
that means the toast shows from a non-uithread.
why?
I am so sorry. I know answer. Toast is a special UI element. It can be showed from any thread. But the other UI elements ,such as Button TextView must only be touched in the UI-thread.
So,my code runs correct,but when you change the toast to Button ,is crashed.
You are trying to show toast in a UI thread using runnable thats why its going wrong
Thread background = new Thread(new Runnable() {
public void run() {
// Send message to handler
handler.sendMessage(msgObj);
}
};
private final Handler handler = new Handler() {
public void handleMessage(Message msg) {
//Catch the response and show the toast
String aResponse = msg.getData().getString("message");
Toast.makeText(getBaseContext(),"Not Got Response From Server.",
Toast.LENGTH_SHORT).show();
}
};
You must create the Handler in UiThread. The handler send the message to thread where it was created.
handler = new Handler();
thread = new Thread() {
public void run() {
super.run();
System.out.println("run:" + Thread.currentThread().getName());
Looper.prepare();
Looper.loop();
};
};
thread.start();

android get user input from activity to another thread

I want to wirte an android app that will read input from user each time i press abutton and will handle it in different thread(not activity) also i dont want to open new thread each time i need an example for that.
Thanks a lot!
Get the text from user input field:
EditText editText = findViewById(R.id.yourEditTextId);
final String text = editText.getText().toString;
Then you could do something like this:
Handler handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
// do something with String text
}
};
handler.post(r);
Or this:
new Thread(new Runnable() {
public void run() {
// do something with String text
}
}).start();
You can find info about threads in Android here: http://developer.android.com/guide/components/processes-and-threads.html
you could use this link here
`Thread thread = new Thread()
{
#Override
public void run() {
try {
// do something here
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();`

Android present streamed images

I want to present bitmap images from a internetstream. Every 500 millisec i get a new image and i want to present this image to the user. What is the best way to do this? Can i use an image view and chenge the image every 500 millisec?
I tried to do this in an timer task like this:
timer.schedule(new TimerTask() {
public void run() {
if(flag){
Bitmap bmp = null;
Log.i(APPID, "New frame");
try {
bmp = session.getImage();
setImage(bmp);
} catch (Exception e) {
e.printStackTrace();
}
} else {
timer.cancel();
}
}
}, 500, 500);
But this does not work.
Updating the UI from a thread other than the UI/main thread will fail as Android does not allow it. Try using a Handler to post messages back to the UI thread. You could do something like this.
final Handler h = new Handler();
timer.schedule(new TimerTask() {
public void run() {
if(flag){
h.post(new Runnable() {
public void run() {
Bitmap bmp = null;
Log.i(APPID, "New frame");
try {
bmp = session.getImage();
setImage(bmp);
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
timer.cancel();
}
}
}, 500, 500);
Timer task runs on a different thread. you need to update ui on the ui thread. You should use runOnUiThread or Handler
runOnUiThread(new Runnable() //run on ui threa
{
public void run()
{
// update ui here
}
});
Handler
Handler m_handler;
Runnable m_handlerTask ;
m_handler= new Handler();
m_handlerTask = new Runnable()
{
#Override
public void run() {
// do soemthing
m_handler.postDelayed(m_handlerTask, 1000);
// change 1000 to whatever you want
}
};
m_handlerTask.run();
When you wan to cancel call this m_handler.removeCallbacks(m_handlerTask).

Categories

Resources