0

I'm new to develop Android Apps and writing java. But, I only want to make a simple app that will give a different output on TextView depending on what time of the day it is.

F.ex. If the time is between 06:45 and 08:45 I want it to say "Good Morning", and so forth.

I have been playing with Eclipse and created a simple WebView-app but I can't seem to find any information to either choose which TextView I want shown on a specific time or show a different layout.

Do you have any tips? This is my .java file in src for now.

package com.wao.texttime;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class TextTime extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tv1 = (TextView) findViewById(R.id.TextView01);
        tv1.setText("Good Morning"); 
    }
}
Anders
  • 633
  • 2
  • 11
  • 21

1 Answers1

3

There is no need to switch between different TextViews. If you just want to display different messages in your TextView I would simply call the setText function to update the TextView's text attribute.

Julian
  • 20,008
  • 17
  • 77
  • 108
  • I need some guidance to do this. Do I define the rules in the .java file in src? And how can I pull the text/or write the texts in the layout-file? – Anders Nov 28 '10 at 17:09
  • I recommend googling for "Android TextView tutorial" or something similar. Following is also a question regarding how to change the text in a TextView: http://stackoverflow.com/questions/2300169/how-to-change-text-in-android-textview – Julian Nov 28 '10 at 17:16
  • Thanks, I will read up on it. Do you have any tips on how I can define the rules regarding defining the text based on the time of the day as well? – Anders Nov 28 '10 at 17:18
  • The `AlarmManager` (http://developer.android.com/reference/android/app/AlarmManager.html) can for instance be used to trigger at given times. You could use these events to update the text in your `TextView` accordingly. – Julian Nov 28 '10 at 17:41
  • If an Activity in the background updates its UI, does anyone notice? :) AlarmManager sounds like wasteful overkill here. For something like this it's probably far simpler to get the current time and update the text when your Activity comes to the foreground in `onResume()`. Unless of course you expect your users to leave your app open with the screen on and unlocked for long enough for it to matter. :) – adamp Nov 28 '10 at 20:50
  • I think you are right adamp. It will be a simple message updating every two hours or so, you will probably only open one time a day. How do I define rules and pull out the current time? – Anders Nov 28 '10 at 21:21
  • @adamp: naturally, checking in `onResume()` should be plenty sufficient if it's in an app that rarely is open. – Julian Nov 28 '10 at 21:28
  • @Anders: comments are for *comments*. If you have something more you wonder, ask a new question. And don't forget to accept the questions you've already gotten answers to. – Julian Nov 28 '10 at 21:29
  • @Nailuj I'm very sorry. New question posted: http://stackoverflow.com/questions/4299234/using-settext-with-rules-regarding-the-current-time – Anders Nov 28 '10 at 21:38
  • @Anders: no apology needed, it can take a bit of time to get along with how things work here on Stack Overflow :) Just take a couple of minutes whenever you've got some time to spare and skim through the FAQ, lots of nice info there: http://stackoverflow.com/faq – Julian Nov 28 '10 at 21:47