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.
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.
Please provide more detailed information here.
-
Answer # 1
Related articles
- android studio - [kotlin] there is activity_loginxml, but i get an unresolved reference: activity_login error
- android emulator - kotlin generics i want to create a method with the return value of any class
- error when launching android studio emulator
- [kotlin] [android studio] null is returned even if findviewbyid of linearlayout
- android emulator - imageview error in android studio
- android emulator - gps implementation android studio
- android studio emulator error
- android studio - display a dialog
- android studio - [kotlin] the value of the variable becomes empty at the call destination of the function
- error in android studio emulator
- how to manage state in android studio kotlin
- android studio emulator does not start
- Android Studio Kotlin code and java code conversion example
- android emulator - android studio refactoring preview method
- how to save ${} as a string in kotlin android studio
- Android Studio kotlin generates editing class comment code
- The implementation of Android studio generation with Kotlin documents
- android studio - kotlin: how to implement a drum roll with selectable strings
- visual studio android emulator does not start and a placement error occurs
- android studio - i would like to know the reason why cannot be converted appears in jsonobject (string) and how to deal with it
- android - i registered a paid app on google play
- android studio - how to change the layout_weight value set in the xml file from the kotlin file
- unable to change the xml layout of an android fragment
- i want to add the characters entered in edittext as listview information by pressing the android button
- i made an android app all that remains is the billing system
- android studio - viewpager and viewadapter cannot be associated
- android studio - null-related error, the application crashes and cannot be resolved
- android studio - in the database sample, i want to prevent the application from crashing even if i click the menu list
- android - the google play console subscription skudetails is empty why?
Isn't AlarmBroadcastReceiver running?
In AndroidManifest.xml
It seems that you need a description such as, but is it missing?
Aside.
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,
You can also replace with an underscore like.