Hide A Layout After 10 Seconds In Android? - android

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

Related

How to update a textview frequently with a spesific time delay?

I need to update a TextView frequently with a specific time delay in the android studio. The code is below. Thank you.
Edit: I also need to end the loop with a button click or with an "if" control.
//INFLATION CALCULATION !!!
/**
* This method calculates Inflation value.
*/
public void calculateInflation() {
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
inflation = (cpi-cpiIni)/cpiIni*100;
displayInflation();
cpiIni = cpi;
}
}, delay*12);
}
Call the same method inside the runnable in order to keep the loop going
Use a flag in order to be able to stop the loop: shouldCalculate
private boolean shouldCalculate = true; // set to false when you want to end the loop
public void calculateInflation() {
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
if (shouldCalculate) {
inflation = (cpi-cpiIni)/cpiIni*100;
displayInflation();
cpiIni = cpi;
calculateInflation();
}
}
}, delay*12);
}
private Runnable updateTimerThread = new Runnable() {
public void run() {
inflation = (cpi-cpiIni)/cpiIni*100;
displayInflation();
cpiIni = cpi;
customHandler.postDelayed(this, 0);
}
};
public void startTimer() {
//timer
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
}
public void stopTimer() {
//timer stops
customHandler.removeCallbacks(updateTimerThread);
//timer ends
}
make a reference of runnable thread , start it using startTimer() and remove thread using stopTimer() as you said on a button click or up on a specific conditions .Also you can change the postDelayed milliseconds as ur wish
Try below code. This will do the trick. If you find any problem please let me know.
public void calculateInflation() {
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
inflation = (cpi-cpiIni)/cpiIni*100;
displayInflation();
cpiIni = cpi;
if(shouldRepeat)
calculateInflation();
}
}, delay*12);
}
And second approach can be CountDownTimer. Make a method as shown in below code
public void timerTask(final int loopTime){
//Loop time is the actual time for repeatation
new CountDownTimer(loopTime, 1000) {
public void onTick(long millisUntilFinished) {
//this tells you one second is passed
}
public void onFinish() {
//here on time finish you need to define your task
inflation = (cpi-cpiIni)/cpiIni*100;
displayInflation();
cpiIni = cpi;
//call the same method again for looping
timerTask(loopTime);
}
}.start();
}
Simplest way. Here updateRunnable calls itself with delay. Make updateRunnable as global variable to access from anywhere.
Runnable updateRunnable = new Runnable() {
#Override
public void run() {
inflation = (cpi-cpiIni)/cpiIni*100;
displayInflation();
cpiIni = cpi;
handler.postDelayed(this, UPDATE_TIME);
}
};
Start handler. Here we start handler immediately without delay.
handler.postDelayed(updateRunnable, 0)
Stop handler
handler.removeCallbacks(updateRunnable)
By the way don't forget to stop handler on onDestroy()

Remove postDelayed in android

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

Hide TextView after some time in Android

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);

Delay actions in android

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);

Hide and make visible button every 5 second interval

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();

Categories

Resources