Home>

I'm studying with Android Studio while reading my first Android programming.
I am trying to enter the sample code to display a dialog, but it does not display well.
I reviewed it many times, but I didn't understand.

override fun onCreateDialog (savedInstanceState: Bundle?): Dialog {
val builder = AlertDialog.Builder (requireActivity ())
builder.setMessage ("It's time")
builder.setPositiveButton ("wakes up") {dialog, which->
listener? .getUp ()
}
builder.setNegativeButton ("5 minutes left") {dialog, which->
listener? .snooze ()
}
return builder.create ()
}

The wavy lines are displayed in gray below dialog, which on the 4th and 7th lines of this code.
There are no other errors.
Is there a different way to write dialogs with the latest version?
I'm a beginner in programming, so I'm sorry if the question is off the mark.

Applicable source code

Kotlin
Source code
MainActivity.kt

package com.example.myalarmclock
import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main. *
import java.util. *
class MainActivity: AppCompatActivity (), TimeAlertDialog.Listener {
    override fun getUp () {
        Toast.makeText (this, "Getting up was clicked", Toast.LENGTH_SHORT)
            .show ()
    }
    override fun snooze () {
        Toast.makeText (this, "5 minutes left clicked", Toast.LENGTH_SHORT)
            .show ()
    }
    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate (savedInstanceState)
        if (intent? .getBooleanExtra ("onReceive", false) == true) {
            val dialog = TimeAlertDialog ()
            dialog.show (supportFragmentManager, "alert_dialog")
        }
        setContentView (R.layout.activity_main)
        setAlarm.setOnClickListener {
            val calendar = Calendar.getInstance ()
            calendar.timeInMillis = System.currentTimeMillis ()
            calendar.add (Calendar.SECOND, 5)
            setAlarmManager (calendar)
            cancelAlarm.setOnClickListener {
             cancelAlarmManager ()
            }
        }
    }
    private fun setAlarmManager (calendar: Calendar) {
        val am = getSystemService (Context.ALARM_SERVICE) as AlarmManager
        val intent = Intent (this, AlarmBroadcastReceiver :: class.java)
        val pending = PendingIntent.getBroadcast (this, 0, intent, 0)
        when {
            Build.VERSION.SDK_INT>= Build.VERSION_CODES.LOLLIPOP->{
                val info = AlarmManager.AlarmClockInfo (
                    calendar.timeInMillis, null)
                am.setAlarmClock (info, pending)}
            Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT->{
                am.setExact (AlarmManager.RTC_WAKEUP,
                            calendar.timeInMillis, pending)
            }
            else->{
                am.set (AlarmManager.RTC_WAKEUP,
                       calendar.timeInMillis, pending)
            }
        }
    }
    private fun cancelAlarmManager () {
        val am = getSystemService (Context.ALARM_SERVICE) as AlarmManager
        val intent = Intent (this, AlarmBroadcastReceiver :: class.java)
        val pending = PendingIntent.getBroadcast (this, 0, intent, 0)
        am.cancel (pending)
    }
}

AlarmBroadcastReceiver.kt

package com.example.myalarmclock
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.widget.Toast
class AlarmBroadcastReceiver: BroadcastReceiver () {
    override fun onReceive (context: Context, intent: Intent) {
        val mainIntent = Intent (context, MainActivity :: class.java)
            .putExtra ("onReceive", true)
            .addFlags (Intent.FLAG_ACTIVITY_NEW_TASK)
        context.startActivity (mainIntent)
    }
}

Dialogs.kt

package com.example.myalarmclock
import android.app.AlertDialog
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.support.v4.app.DialogFragment

class TimeAlertDialog: DialogFragment () {
    interface Listener {
        fun getUp ()
        fun snooze ()
    }
    private var listener: Listener? = null
    override fun onAttach (context: Context) {
        super.onAttach (context)
        when (context) {
            is Listener->listener = context
        }
    }
    override fun onCreateDialog (savedInstanceState: Bundle?): Dialog {
        val builder = AlertDialog.Builder (requireActivity ())
        builder.setMessage ("It's time")
        builder.setPositiveButton ("wakes up") {dialog, which->            listener? .getUp ()
        }
        builder.setNegativeButton ("5 minutes left") {dialog, which->            listener? .snooze ()
        }
        return builder.create ()
    }
}

activity_main.xml

<? xml version = "1.0" encoding = "utf-8"?>    <ImageView
        android: id = "@ + id/imageView"
        android: layout_width = "0dp"
        android: layout_height = "0dp"
        android: scaleType = "centerCrop"
        app: layout_constraintBottom_toBottomOf = "parent"
        app: layout_constraintEnd_toEndOf = "parent"
        app: layout_constraintHorizontal_bias = "0.0"
        app: layout_constraintStart_toStartOf = "parent"
        app: layout_constraintTop_toTopOf = "parent"
        app: layout_constraintVertical_bias = "0.0"
        app: srcCompat = "@ drawable/background" />    <TextView
        android: id = "@ + id/dateText"
        android: layout_width = "0dp"
        android: layout_height = "wrap_content"
        android: layout_marginStart = "32dp"
        android: layout_marginTop = "32dp"
        android: layout_marginEnd = "32dp"
        android: background = "@ color/textBackground"
        android: textColor = "@ android: color/white"
        android: textSize = "36sp"
        app: layout_constraintEnd_toEndOf = "parent"
        app: layout_constraintStart_toStartOf = "parent"
        app: layout_constraintTop_toTopOf = "parent" />    <TextView
        android: id = "@ + id/timeText"
        android: layout_width = "0dp"
        android: layout_height = "wrap_content"
        android: layout_marginStart = "32dp"
        android: layout_marginTop = "32dp"
        android: layout_marginEnd = "32dp"
        android: background = "@ color/textBackground"
        android: textAlignment = "center"
        android: textColor = "@ android: color/white"
        android: textSize = "36sp"
        app: layout_constraintEnd_toEndOf = "parent"
        app: layout_constraintStart_toStartOf = "parent"
        app: layout_constraintTop_toBottomOf = "@ + id/dateText" />    <Button
        android: id = "@ + id/setAlarm"
        android: layout_width = "wrap_content"
        android: layout_height = "wrap_content"
        android: layout_marginStart = "32dp"
        android: layout_marginTop = "32dp"
        android: layout_marginEnd = "32dp"
        android: text = "@ string/setAlarm"
        app: layout_constraintEnd_toEndOf = "parent"
        app: layout_constraintStart_toStartOf = "parent"
        app: layout_constraintTop_toBottomOf = "@ + id/timeText" />    <Button
        android: id = "@ + id/cancelAlarm"
        android: layout_width = "wrap_content"
        android: layout_height = "wrap_content"
        android: layout_marginStart = "32dp"
        android: layout_marginTop = "32dp"
        android: layout_marginEnd = "32dp"
        android: text = "@ string/cancerAlarm"
        app: layout_constraintEnd_toEndOf = "parent"
        app: layout_constraintStart_toStartOf = "parent"
        app: layout_constraintTop_toBottomOf = "@ + id/setAlarm" /></android.support.constraint.ConstraintLayout>
What I tried

val builder = AlertDialog.Builder (requireActivity ())
I tried changing requireActivity () to requireContext () and so on.

Supplemental information (FW/tool version, etc.)

Please provide more detailed information here.

  • Answer # 1

    Isn't AlarmBroadcastReceiver running?

    In AndroidManifest.xml

            <receiver android: name = ". AlarmBroadcastReceiver" />

    It seems that you need a description such as, but is it missing?


    Aside.

            builder.setPositiveButton ("wakes up") {dialog, which->


    A yellow wavy line (which may look gray) is attached to the dialog and which in which is because these arguments are unused. If you care,

            builder.setPositiveButton ("wakes up") {_, _->


    You can also replace with an underscore like.