I want to change the image in imageView after 5 seconds from app start.
This is the code that I tried so far:
public class MainActivity extends Activity {
ImageView screen;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screen=(ImageView)findViewById(R.id.imageView1);
screen.setImageResource(R.drawable.ic_launcher);
}
}
You can use a Handler, such as:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// change image
}
}, 5000); // 5000ms delay
As Sam says in the comments, you could also do this (because all Views have their own handler):
screen.postDelayed(new Runnable() {
#Override
public void run() {
// change image
}
}, 5000); // 5000ms delay
See the Handler Documentation.
you can try thread like this:
new Thread(){
public void run(){
//sleep(5000);
//refreshSthHere();
}
}.start();
You can use Handler like Tom Leese said or Java Timer
new Timer().schedule(new TimerTask() {
#Override
public void run() {
// this code will be executed after 2 seconds
}
}, 2000);
Related
I'm trying to change some textview value but when it's not appeared on the screen the value don't change or when it's appeared on screen it changes and when scroll down and scroll back up it's value returns to the old one i tried the following two ways but non of them is working :
final Timer timer=new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
myTextView.setText("sometext");
timer.cancel();
}
});
}
}, 5000, 1000);
and:
MainActivity.this.r1.post(new Runnable() {
public void run() {
myTextView.setText("sometext");
}
});
Try using Handler
private void TestThread() {
Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
handler.post(new Runnable(){
public void run() {
tvTime.setText("SomeText");
}
});
}
}
};
new Thread(runnable).start();
}
Also you can take a look AsyncTask Documentation and onPostExecute() you set the text to TextView.
You have to make sure that the UI is drawn first before you change the value of the TextView , It can be done by adding a layout listener,
ViewTreeObserver vto = getWindow().getDecorView().getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Overrid
public void onGlobalLayout() {
final Timer timer=new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
myTextView.setText("sometext");
timer.cancel();
}
});
}
}, 5000, 1000);
}
}
I am trying to create one application which checks battery status every one minute and update the UI with the battery Level.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
batteryPercent = (TextView) this.findViewById(R.id.battery);
while (true) {
runOnUiThread(mRunnable);
}
}
private Runnable mRunnable = new Runnable() {
#Override
public void run() {
getBatteryPercentage();
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
`getBatteryPercentage()1 update a text view on UI.
When I call getBatteryPercentage() only once the code works fine, but when I try to run it in a loop like above, after few seconds I get Application Not Responding(ANR).
Is there any way to make the app wait for 60 seconds without getting ANR?
Don't do it with Sleep. Use a CountDownTimer instead.
CountDownTimer _timer;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
batteryPercent = (TextView) this.findViewById(R.id.battery);
_timer = new CountDownTimer(Long.MAX_VALUE, 60000) {
#Override
public void onTick(long millisUntilFinished)
{
getBatteryPercentage();
}
#Override public void onFinish() {}
};
_timer.start();
Don't forget to call _timer.cancel() before the Activity exits.
You can use Handler.postDelayed for this.
Handler handler = new Handler();
private Runnable mRunnable = new Runnable() {
#Override
public void run() {
getBatteryPercentage();
handler.postDelayed(mRunnable, 60000);
}
}
And then:
handler.postDelayed(mRunnable, 60000);
if you do something in android uithread more than 5 seconds,the application will show ANR toast.
you should do while loop in another thread,and use callback to refresh ui.you can do it like this:
new Thread(new Runnable(){
public void run(){
try{
Thread().sleep(6*1000);
updateUI();
}catch( Exception e){
e.print***();
}}}).start();
private void updateUI(){
runOnUiThread(new Runnable(){
getBatteryPercentage();
});
}
I am using postDelayed with TextView to hide it after some time. Now, I want to remove postDelayed if user click on button.
My code is as below :
tvRQPoint.setText("+100");
tvRQPoint.postDelayed(new Runnable() {
public void run() {
tvRQPoint.setText("");
}
}, 10000);
How to do this ?
Create your thread in separate place below...
private Runnable mTimerExecutor = new Runnable() {
#Override
public void run() {
tvRQPoint.setText("");
}
};
Then call it as follows to execute....
tvRQPoint.postDelayed(mTimerExecutor, 10000);
When you want to cancel the postDelay execution then cancel as follows...
tvRQPoint.removeCallbacks(mTimerExecutor);
check this
Runnable runnable = new Runnable() {
public void run() {
tvRQPoint.setText("");
}
};
tvRQPoint.setText("+100");
tvRQPoint.postDelayed(runnable, 10000);
to remove it
tvRQPoint.removeCallbacks(runnable);
boolean clicked=false;
onClick event
clicked=true;
and in postDelayed
tvRQPoint.setText("+100");
tvRQPoint.postDelayed(new Runnable() {
public void run() {
if(!clicked)
tvRQPoint.setText("");
}
}, 10000);
Use below code inside onClick. It will remove.
private final Runnable r = new Runnable() {
public void run() {
tvRQPoint.setText("");
Handler handler = new Handler();
handler.postDelayed(this, 2000);
}
}
};
and then use this inside onClick of button
handler.removeCallbacks(r);
For more information check this link
I would like to know if it's possible to use handler().postdelayed twice?
I mean, I want to create a button, that when clicked it change the color and stay in this state 1 second, then, after 1 second another button change the color.
I've created the following code:
In the onclicklistener:
btn3.setBackgroundColor(Color.WHITE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
checkAnswer();
waitAnswer();
btnRsp3.setBackgroundResource(R.drawable.selector);
}
}, 1000);
CheckAnswer:
public void CheckAnswer(){
btn1.setBackgroundColor(Color.GREEN);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
}
}, 500);
btn1.setBackgroundResource(R.drawable.selector);
}
I think the problem is on CheckAnswer because it seems it doesn't stop in this postDelayed and step to the waitAnswer.
Thanks
Why do you expect it to stop on postDelayed? postDelayed places your Runnable to the Handler Looper queue and returns. Since both handlers are created on the same looper, the second runnable is executed after the first one terminates (plus whatever left of the 500 ms delay)
UPDATE:
You need something like that
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
btn1.setBackgroundColor(Color.GREEN);
}
}, 1000);
handler.postDelayed(new Runnable() {
#Override
public void run() {
btn1.setBackgroundResource(R.drawable.selector);
}
}, 2000);
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
//Your Work
}
}, 1000);
I have a layout displayed on a button click.I want to hide that layout after 10 seconds.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mVolHandler = new Handler();
mVolRunnable = new Runnable() {
public void run() {
mVolLayout.setVisibility(View.GONE);
}
};
}
private OnTouchListener mVolPlusOnTouchListener = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
mVolLayout.setVisibility(View.VISIBLE);
mVolHandler.postDelayed(mVolRunnable, 10000);
}
}
Make use of Handler & Runnable.
You can delay a Runnable using postDelayed of Handler.
Runnable mRunnable;
Handler mHandler=new Handler();
mRunnable=new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
yourLayoutObject.setVisibility(View.INVISIBLE); //If you want just hide the View. But it will retain space occupied by the View.
yourLayoutObject.setVisibility(View.GONE); //This will remove the View. and free s the space occupied by the View
}
};
Now inside onButtonClick event you have to tell Handler to run a runnable after X milli seconds:
mHandler.postDelayed(mRunnable,10*1000);
If you want to cancel this then you have to use mHandler.removeCallbacks(mRunnable);
Update (According to edited question)
You just need to remove callbacks from Handler using removeCallbacks()
So just update your code inside onTouch method like this :
mVolLayout.setVisibility(View.VISIBLE);
mVolHandler.removeCallbacks(mVolRunnable);
mVolHandler.postDelayed(mVolRunnable, 10000);
You can use an Animation started when you click the button, with 10 seconds duration that fades out the layout and probably sets its visibility to GONE at the end.
You can use postDelayed:
val delay = 3000L // 3 seconds
view.postDelayed({ view.visibility = View.GONE }, delay)
Use Handler to hide layout after 10 seconds. Use Post delayed method
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
pd=ProgressDialog.show(this,"","Loading, Please wait .. ",true);
setTitle("set title");
final Handler uiThreadCallback=new Handler();
final Runnable runInUIThread= new Runnable(){
public void run(){
fnDraw();
pd.dismiss();
}
};
new Thread(){
#Override public void run(){
uiThreadCallback.post(runInUIThread);
}
}.start();
}
public void fnDraw(){
setContentView(R.layout.define ur xml if any);
i1=(ImageView)findViewById(R.id.i1);
t1=(TextView)findViewById(R.id.t1);
t2=(TextView)findViewById(R.id.t2);
timerRegister=new Timer();
lIteration=0;
checkTime=new TimerTask(){
public void run(){
if(lIteration==1){
timerRegister.cancel();
uiDrawThreadCallback.post(runInUIDrawThread);
}
lIteration++;
return;
}
};
timerRegister.scheduleAtFixedRate(checkTime,0,10000);
}
final Handler uiDrawThreadCallback=new Handler();
final Runnable runInUIDrawThread= new Runnable(){
#Override
public void run(){
fnDrawAgain();
}
};
public void fnDrawAgain(){
Intent intent=new Intent(this,new class you want to open.class);
startActivity(intent);
}
}
try it m sure it gonna work in ur on create screen