I want to hide TextView after some time interval say 3 seconds. I googled and found some code and I tried code as shown below but It is not working.
Please tell me what is the wrong with this ?
tvRQPoint.setText("+0");
tvRQPoint.postDelayed(new Runnable() {
public void run() {
tvRQPoint.setText("+0");
}
}, 3000);
One more thing, how to remove timeout ? As I am using this on click event of ListView, If user clicks on one option and then clicks on second option, then as 3 seconds got over (after clicked on first option), It does not show second option for 3 seconds.
try View INVISIBLE or GONE like:
tvRQPoint.postDelayed(new Runnable() {
public void run() {
tvRQPoint.setVisibility(View.INVISIBLE);
}
}, 3000);
Set View visibility with view.setVisibility(View.INVISIBLE|View.VISIBLE|View.GONE);
How about hiding your text view with some animation?
int delayMillis = 3000;
Handler handler = new Handler();
final View v = tvRQPoint; // your view
handler.postDelayed(new Runnable() {
#Override
public void run() {
TranslateAnimation animate = new TranslateAnimation(0,-view.getWidth(),0,0);
animate.setDuration(500);
animate.setFillAfter(true);
v.startAnimation(animate);
v.setVisibility(View.GONE);
}, delayMillis);
What you are trying to do is ok but after three seconds you want to hide the textview so use setVisibility
tvRQPoint.setText("+0");
tvRQPoint.postDelayed(new Runnable() {
public void run() {
tvRQPoint.setVisibility(View.INVISIBLE);
}
}, 3000);
try this...
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv=(TextView)findViewById(R.id.tv);
tv.setText("+0");
tv.postDelayed(new Runnable() {
public void run() {
tv.setVisibility(View.INVISIBLE);
}
}, 3000);
}
}
I simply animated the View from alpha 1 to 0, meaning visibility from 100% to 0%.
scrollUp = findViewById(R.id.lottieAnimationView);
scrollUp.postDelayed(new Runnable() {
#Override
public void run() {
AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
alphaAnimation.setDuration(400);
scrollUp.startAnimation(alphaAnimation);
scrollUp.setVisibility(View.INVISIBLE);
}
},5000);
I am using the LottieAnimationView but same is applicable with any other view
just replace the id in R.id.{ID OF YOUR VIEW}
alphaAnimation.setDuration(400); This means the animation will take 4 seconds to complete i.e higher this number the slower the animation.
},5000);
and the "5000" means 5 seconds delay.
For every second of delay you put it as 1000 ms or milliseconds of delay.
1 Seconds = 1000 ms
Hope it helps someone, all other answers are correct as well, this is just another way of doing it.
Try this-
Handler handler;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
tvRQPoint.setText("+0");
handler = new Handler();
handler.postDelayed(csRunnable, 3000);
}
Runnable csRunnable =new Runnable()
{
#Override
public void run()
{
tvRQPoint.setVisibility(View.INVISIBLE);
}
};
You are setting the text in run() method. you can hide the text in two ways
View.INVISIBLE - give the space for the textview and hide its content
View.GONE - remove the space for textview and hide its content
so call
tvRQPoint.setVisibility(View.INVISIBLE);
(or)
tvRQPoint.setVisibility(View.GONE);
hope it's work:
tvRQPoint.setText("+0");
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
tvRQPoint.setVisibility(View.GONE);
}
},3000);
Related
In my app I want to make a button invisible for a few seconds after another button has been pressed and then it should become visible again.
How it is possible?
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.setVisibility(View.INVISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
btn.setVisibility(View.VISIBLE);
}
}, 1000); // where 1000 is equal to 1 sec (1 * 1000)
}
});
You can do somthing like this:
firstBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
secondBtn.setVisibility(View.INVISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
secondBtn.setVisibility(View.VISIBLE);
}
}, 2000); //change it for the time you need in milliseconds
}
});
must make buttonView invisible then use btnView.postDelayed
Just inside onClick of second button just do
secondButtonView.setVisibility(View.INVISIBLE);
secondButtonView.postDelayed(new Runnable() {
#Override
public void run() {
secondButtonView.setVisibility(View.VISIBLE);
}
}, 2000);
View.postDelayed() simply calls Handler.postDelayed(). It's a
convenient method that helps avoid creating Handler instances.
This quote is from Romain Guy Android framework engineer https://groups.google.com/forum/#!topic/android-developers/IuG3HgKx89Q
//my button invisible
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// Do something after 5s = 5000ms
//my button visible
}
}, 5000);
U can use handler for it
also u can use Timer and TimerTask
//First button invisible
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// Second visible
// after some MS
}
}, 2000);
Suppose you have two buttons Button button1,button2 properly inflated and displayed in view. You can change visibility of button2 on click of button1 by:
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
button2.setVisibility(View.INVISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
button2.setVisibility(View.VISIBLE);
}
}, 2 * 1000);//number of seconds *1000
}
});
try this,
write following code on another button's click event.
continuebutton.setVisibility(View.INVISIBLE);
continuebutton.postDelayed(new Runnable() {
public void run() {
continuebutton.setVisibility(View.VISIBLE);
}
}, 2000);
i try to rotate layout with handler.i wrote some code
void handlechange() {
Handler hand = new Handler();
hand.postDelayed(new Runnable() {
#Override
public void run() {
change();
}
private void change() {
myImage.setRotation(5);
handlechange();
}
}, 500);
}
i can rotate layout but i want to rotate layout every time. 500 milisecond.
setRotate working only one time.
how i can solve my problem? if anyone knows solution please help me
This will rotate any view you want. Will increment 5 to it's rotation every 500ms. Change the values to what you want.
public void rotateImage(final View myView) {
final Handler myHandler = new Handler();
myHandler.postDelayed(new Runnable() {
#Override
public void run() {
myView.setRotation(myView.getRotation() + 5);
rotateImage(myView);
}
}, 500);
}
Notice the myView.getRotation() + 5. Otherwise you're setting the rotation to 5 every time.
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);
Can it possible to make a button on screen which automatically visible and gone every 5sec interval?
By using this
b.setVisibility(View.VISIBLE);
we can visible and
b.setVisibility(View.GONE);
we can hide it.But I can't manage to make it by usin the time interval.
Any idea?please share.
There are a few different ways, one is a Handler and Runnable:
public class Example extends Activity {
private Handler mHandler = new Handler();
private Runnable alternate = new Runnable() {
public void run() {
// Alternate visible and not
mHandler.postDelayed(alternate, 5000);
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mHandler.postDelayed(alternate, 5000);
}
}
Use this
new CountDownTimer(9000000, 5000) {
public void onTick(long millisUntilFinished) {
if(b.getVisibility() == View.GONE)
b.setVisibility(View.VISIBLE);
else
b.setVisibility(View.GONE);
}
public void onFinish() {
//Restart timer if you want.
}
}.start();
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