after thread sleep TextView is not printing vlaue - android

tv value set to value 1 then after 5000 it does not set its value , showdown the app
tv = (TextView) findViewById(R.id.textView1);
Thread time = new Thread(){
public void run(){
try{
tv.setText("Value 1");
sleep(5000);
tv.setText("Value 2");
}
catch ( InterruptedException e){
e.printStackTrace();
}finally{
tv.setText("Value 3");
}
}
};
time.start();

You should call Activity.runOnUiThread() to update the UI from other threads.
Thread t = new Thread() {
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
tv.setText("Value 1");
}
});
}
};
OR
Use a Handler:
Thread t =new Thread(){
public void run() {
handler.post(new Runnable() {
public void run() {
tv.setText("Value 1");
}
});
}
}};

You have to run the code that deals with UI on the main thread... it's called UIThread... you can do any thing on another threads except handling UI
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
tv.setText....
}
});

You have to run the TextView update method inside the runOnUiThread() method. Your code should look something like this:
Thread time = new Thread() {
public void run() {
try {
runOnUiThread(new Runnable() {
#Override
public void run() {
tv.setText("Value 1");
}
});
sleep(5000);
runOnUiThread(new Runnable() {
#Override
public void run() {
tv.setText("Value 2");
}
});
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
tv.setText("Value 3");
}
}
};
time.start();

Related

How to pass variables to UI thread from worker thread?

Below is the code for simple get request and res variable is not available inside Ui thread. How this can be achieved in android?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread thread = new Thread(new Runnable(){
#Override
public void run(){
try {
String res = Utils.GetRequest("http://www.google.com");
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(MainActivity.this, res, Toast.LENGTH_SHORT).show();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();
}
Okay, I found out that I haven't declared the res variable as final.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread thread = new Thread(new Runnable(){
#Override
public void run(){
try {
final String res = Utils.GetRequest("http://www.google.com");
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(MainActivity.this, res, Toast.LENGTH_SHORT).show();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();
}
You have to create a handler on the Main thread in the OnCreate()
Handler mHandler = new Handler(Looper.getMainLooper()) {
#Override
public void handleMessage(Message inputMessage) {
// handle the passed value here
// for ex. update the UI by getting the data from the inputMessage
}
}
inside your Thread .. call the
Message myMessage = mHandler.obtainMessage();
myMessage.obj = "the value to update the ui";
mHandler.sendMessage(myMessage);

how to create image slideshow in android

i am calling this medthod show() in onCreate()
, whats wrong in my code
public void show()
{
Toast.makeText(getApplicationContext(),"1",Toast.LENGTH_SHORT).show();
new Thread(new Runnable() {
#Override
public void run() {
Looper.prepare();
Toast.makeText(getApplicationContext(),"2",Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(),"3",Toast.LENGTH_SHORT).show();
int arr[]={R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4};
int i=1;
do
{
i++;
try
{
imageView.setImageResource(arr[i]);
Thread.sleep(2000);
}
catch (InterruptedException e) {e.printStackTrace();}
}
while(i==arr.length);
}
},2000);
}
}).start();
}
i think it should wait for 2 sec. every time and show next image but its doing nothing even no error
i used toast to counter processing then i found its only reaching to first toast

Stop runnable on button click

I have Runnable and have two Handlers for button background color change after some music is played.
Now how can I stop the music played using runnable for a ever delay of 700 mili sec on a button click named stop.
handler=new Handler();
runnable = new Runnable() {
#Override
public void run() {
for (int i = 0; i<Uirkeys1.length; i++) {
final int value = i;
try {
Thread.sleep(700);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
#Override
public void run() {
copyView.get(cnt1).getBackground().setAlpha(100);
ModeSound(Integer.parseInt(CopyMultimap.get(copykey[cnt1]).get(3)));
}
});
try {
Thread.sleep(800);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
#Override
public void run()
copyView.get(cnt1).getBackground().setAlpha(500);
cnt1++;
}
});
}
}
};
new Thread(runnable).start();
cnt1=0;
}
});
}
You could create a class MyRunnable that implements Runnable, and create a field in it
private boolean stopped = false;
public void stop() { stopped = true; }
And then you should check in your execution loop
if(!stopped) {..do work..} else return .
And you can of course call the stop() method on your MyRunnable object.

timer.setText("setTextHere") does not work inside the thread

timer.setText("setTextHere") does not work inside the thread.
Thread thread1 = new Thread(){
TextView timer;
int t;
public void run(){
timer=(TextView) findViewById(R.id.timer);
try{
timer.setText("setTextHere");
sleep(5000);
}
catch(Exception e){
e.printStackTrace();
}
finally{
Intent new1 = new Intent("com.example.app1.MENU");
startActivity(new1);
}
}
};
thread1.start();
_t = new Timer();
_t.scheduleAtFixedRate( new TimerTask() {
#Override
public void run() {
_count++;
runOnUiThread(new Runnable() //run on ui thread
{
public void run()
{
_tv.setText( "" + _count );
}
});
}
}, 1000, 1000 );
You cannot touch UI from background thread. Try to use AsyncTask http://developer.android.com/reference/android/os/AsyncTask.html
On your onCreate() add:
h = new Handler();
add this somewhere:
class SetText implements Runnable {
public void run() {
// do UI task
timer.setText("setTextHere");
}
}
on someFunction() modify like this:
...
try
{
h.post( new SetText() );
sleep(5000);
}
...
final TextView timer = (TextView) findViewById(R.id.timer);
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
timer.post(new Runnable() {
public void run() {
timer.setText("setTextHere");
}
});
} catch (Exception e) {
e.printStackTrace();
} finally {
Intent new1 = new Intent("com.example.app1.MENU");
startActivity(new1);
}
}
}).start();
you could use this and initialize the setText before initializing the Thread
Thread thread1 = new Thread(){
int t;
public void run(){
try{
sleep(5000);
}
catch(Exception e){
e.printStackTrace();
}
finally{
Intent new1 = new Intent("com.example.app1.MENU");
startActivity(new1);
}
}
};
TextView timer;
timer=(TextView) findViewById(R.id.timer);
timer.setText("setTextHere");
thread1.start();

CalledFromWrongThreadException

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ThraedDemo objDemo = new ThraedDemo();
Thread objThread = new Thread() {
#Override
public void run() {
objDemo.firstMethod();
}
};
objThread.start();
}
class ThraedDemo {
private void firstMethod() {
Thread objThread = new Thread() {
#Override
public void run() {
try {
((ImageView)findViewById(R.id.ImageViewnumber)).setImageResource(nums[n]);
Thread.sleep(10000);
Log.v("Thread","1111111111111111sleep");
} catch (InterruptedException ex) {
System.out.println("interuped exception" + ex.getMessage());
}
secondMethod();
}
private void secondMethod() {
Thread objThread = new Thread() {
#Override
public void run() {
try {
((ImageView)findViewById(R.id.ImageViewResult)).setImageResource(nums[n+1]);
n++;
Thread.sleep(10000);
Log.v("Thread","22222222222 sleep");
} catch (InterruptedException ex) {
System.out.println("interuped exception" + ex.getMessage());
}
firstMethod();
}
};
objThread.start();
}
};
objThread.start();
}
}
I use the above code but it is not running.it got CalledFromWrongThreadException what is the problem inb the above code.Please give me some suggestions.Thanks in advance
I think you can't do view modifications from another thread than the UI thread, so either create handlers in the oncreate and post your thread to it, or use AsyncTask, or runOnUIThread method to send portions of code directly to the UI thread.
I edited your 2nd function code, I see your code is loop forever cause the firstMethod call secondMethod and the secondMethod call the new firstMethod to start and then loop forever. I removed it and moved the code update ImageView into the UI Thread, could you try this:
class ThraedDemo {
private void firstMethod() {
Thread objThread = new Thread() {
#Override
public void run() {
try {
runOnUiThread(new Runnable() {
public void run(){
((ImageView)findViewById(R.id.ImageViewnumber)).setImageResource(nums[n]);
}
});
Thread.sleep(10000);
Log.v("Thread","1111111111111111sleep");
} catch (InterruptedException ex) {
System.out.println("interuped exception" + ex.getMessage());
}
secondMethod();
}
};
objThread.start();
}
private void secondMethod() {
Thread objThread2 = new Thread() {
#Override
public void run() {
try {
runOnUiThread(new Runnable() {
public void run(){
((ImageView)findViewById(R.id.ImageViewnumber)).setImageResource(nums[n+1]);
}
});
n++;
Thread.sleep(10000);
Log.v("Thread","22222222222 sleep");
} catch (InterruptedException ex) {
System.out.println("interuped exception" + ex.getMessage());
}
}
};
objThread2.start();
}
}

Categories

Resources