I'm pretty new over here.
I'd like to know how to display a TextView with the text "open" and change it to "closed" depending on the system hour.
I hope my question is clear. Thanks in advance.
I'm pretty new over here.
I'd like to know how to display a TextView with the text "open" and change it to "closed" depending on the system hour.
I hope my question is clear. Thanks in advance.
Use this answer to get the time.
Then do your logic according to the time and day. Then use the function 'setText' of the text view in order to change the text according to the hour.
In case that you want the text to change while the app is still running, you would need to create an event based on time.
on this link there are so many ways to calculate time,
and as per your requirement use if and esle if condition
and in these if and else if conditions
if you want to make your textview invisible
TextView tv1 = (TextView)findViewById(R.id.tv1);
tv1.setVisibility(View.INVISIBLE);
and for making visible
tv1.setVisibility(View.VISIBLE);
I think you can use something like this, sometimes we need to implement things in our applications, that nobody else understands, finally!
you can see full article here: http://android-developers.blogspot.com.br/2007/11/stitch-in-time.html
private Handler mHandler = new Handler();
...
OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
if (mStartTime == 0L) {
mStartTime = System.currentTimeMillis();
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.postDelayed(mUpdateTimeTask, 100);
}
}
};
A couple of things to take note of here. First, the event doesn't have a .getWhen() method on it, which we handily used to set the start time for the timer. Instead, we grab the System.currentTimeMillis(). Also, the Handler.postDelayed() method only takes one time parameter, it doesn't have a "repeating" field. In this case we are saying to the Handler "call mUpdateTimeTask() after 100ms", a sort of fire and forget one time shot. We also remove any existing callbacks to the handler before adding the new handler, to make absolutely sure we don't get more callback events than we want.
But we want it to repeat, until we tell it to stop. To do this, just put another postDelayed at the tail of the mUpdateTimeTask run() method. Note also that Handler requires an implementation of Runnable, so we change mUpdateTimeTask to implement that rather than extending TimerTask. The new clock updater, with all these changes, looks like this:
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
final long start = mStartTime;
long millis = SystemClock.uptimeMillis() - start;
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
if (seconds < 10) {
mTimeLabel.setText("" + minutes + ":0" + seconds);
} else {
mTimeLabel.setText("" + minutes + ":" + seconds);
}
mHandler.postAtTime(this,
start + (((minutes * 60) + seconds + 1) * 1000));
}
};
you can get the current hour and save it in integer variable and use (if)
int hour = new Time(System.currentTimeMillis()).getHours();
if (hour >= 10 && hour <= 19)
textview.setText("OPEN");
else
textview.setText("CLOSE");
I hope thats help