-3

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.

AMayorga
  • 3
  • 1
  • means for one hour you want to display textview n for other you want not to display it – nawaab saab Jul 20 '14 at 08:25
  • Not exactly, what I want is to display the "open" TextView from 10 am to 7 pm, and from 7 pm to 10 am, to display the "closed" TextView – AMayorga Jul 20 '14 at 08:28
  • An if-statement might help. – Henry Jul 20 '14 at 08:30
  • means in your app from morning 10 am to 7 pm the textview in your app must be visible or show the text "open", explain your requirement fully – nawaab saab Jul 20 '14 at 08:31
  • @DKDNZ, I do not know what you want to do, the more u can use Runnable running repeatedly, for instance every minute then do what u want – Anderson K Jul 20 '14 at 08:33

4 Answers4

0

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.

Community
  • 1
  • 1
nheimann1
  • 2,348
  • 2
  • 21
  • 33
0

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);
Community
  • 1
  • 1
nawaab saab
  • 1,892
  • 2
  • 20
  • 36
0

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));
   }
};
Anderson K
  • 5,445
  • 5
  • 35
  • 50
0

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

chazrmani
  • 122
  • 11