Search code examples
androidkotlinbroadcastreceiver

Updating a clock in main activity


Im trying to get into Android Development and want to create a simple app, that shows the current time in a TextView and updates every minute.
I managed to get the time initialized, but can't figure out how to update it.

I googled a lot and am now trying to create a broadcast receiver that updates the TextView.
That's what I got so far:

AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.alarmcompanion">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".TimeBroadcastReceiver"  android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.TIME_SET"/>
                <action android:name="android.intent.action.TIMEZONE_CHANGED"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

MainActivity.kt

package com.example.alarmcompanion

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        clock.text = getString(R.string.time, Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE))

    }
}
class TimeBroadcastReceiver : BroadcastReceiver() {
    override fun onReceive( context: Context, intent: Intent){
        clock.text = getString(R.string.time, Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE))
    }
}

The debugger says, that clock and getString are unresolved references, but I can't seem to figure out how to fix that.
For clock I get this error:

    Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
    public val Activity.clock: TextView! defined in kotlinx.android.synthetic.main.activity_main
    public val Dialog.clock: TextView! defined in kotlinx.android.synthetic.main.activity_main
    public val android.app.Fragment.clock: TextView! defined in kotlinx.android.synthetic.main.activity_main
    public val androidx.fragment.app.Fragment.clock: TextView! defined in kotlinx.android.synthetic.main.activity_main

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/clock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:clickable="false"
        android:gravity="center"
        android:text="@string/time"
        android:textColor="@android:color/background_light"
        android:textSize="120sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Please let me know if I need to provide more info.

Any help or explanation would be greatly appreciated.
Am I even on the right track?


Solution

  • Should anybody stumble across this post, I found the answer here: https://stackoverflow.com/a/13059819/9552448

    The code seems to belong into the MainActivity class.