skip to Main Content

Here is the screenshot of my app

I am making an app in kotlin language. I want to change that Good morning textview to good evening when time is between 4 pm to 8pm and goodnight when time is in range of night time and agin to good morning when time pasts 12 am.

how can i do it in kotlin?

2

Answers


  1. Simply get hour like this:

    val currentHour = Calendar.getInstance().get(Calendar.HOUR)
    

    And set text, based on the hour.

    text.text = if(currentHour > 20 && currentHour < 4) "Good evening" else "Good morning"
    
    Login or Signup to reply.
  2. //Set greeting

        val greetingtext = findViewById<TextView>(R.id.greeting)
        val currentHour = Calendar.getInstance().get(Calendar.HOUR)
        greetingtext.text = if(currentHour < 13 && currentHour < 4) "Good afternoon!" else "Good morning!"
        greetingtext.text = if(currentHour < 18 && currentHour < 18 ) "Good evening!" else "Good evening!"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search