Home > Blockchain >  How to import AlertDialog in Kotlin?
How to import AlertDialog in Kotlin?

Time:11-20

I tried to google my problem but I couldn't found it, that's why I ask.

Error: unresolved reference: AlertDialog

Question: How to import AlertDialog because if I google it then I see different methods that causing the error above, e.g. import android.app.AlertDialog or import androidx.appcompat.app.AlertDialog or something else, I need something universal that will never break. I'm wondering why the #include <...> in C never get expired and lasts for many years.

Import from https://geeksforgeeks.org/how-to-create-a-custom-yes-no-dialog-in-android-with-kotlin/

import android.app.AlertDialog

Import from https://www.geeksforgeeks.org/how-to-create-an-alert-dialog-box-in-android/

import androidx.appcompat.app.AlertDialog;

Import from https://www.digitalocean.com/community/tutorials/android-alert-dialog-using-kotlin

import android.support.v7.app.AlertDialog;

Code in "MainActivity.kt" (Kotlin with C in Android Studio Dolphin | 2021.3.1 Patch 1)

package com.emcengine.emceditor

import android.app.NativeActivity
import android.os.Bundle
import android.content.Context
import android.view.inputmethod.InputMethodManager
import android.view.KeyEvent
import java.util.concurrent.LinkedBlockingQueue

//_/--------------------------------------------------\_
import android.app.AlertDialog // error: unresolved reference: AlertDialog
// \--------------------------------------------------/

class MainActivity : NativeActivity() {
    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    fun showSoftInput() {
        val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.showSoftInput(this.window.decorView, 0)
    }

    fun hideSoftInput() {
        val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.hideSoftInputFromWindow(this.window.decorView.windowToken, 0)
    }

    // Queue for the Unicode characters to be polled from native code (via pollUnicodeChar())
    private var unicodeCharacterQueue: LinkedBlockingQueue<Int> = LinkedBlockingQueue()

    // We assume dispatchKeyEvent() of the NativeActivity is actually called for every
    // KeyEvent and not consumed by any View before it reaches here
    override fun dispatchKeyEvent(event: KeyEvent): Boolean {
        if (event.action == KeyEvent.ACTION_DOWN) {
            unicodeCharacterQueue.offer(event.getUnicodeChar(event.metaState))
        }
        return super.dispatchKeyEvent(event)
    }

    fun pollUnicodeChar(): Int {
        return unicodeCharacterQueue.poll() ?: 0
    }

    //_/--------------------------------------------------\_
    fun messageBox() {
        AlertDialog.Builder builder // error: unresolved reference: AlertDialog
        //builder = new AlertDialog.Builder(this)
    }
    // \--------------------------------------------------/
}

CodePudding user response:

In general: You don't have to add the imports by hand. AndroidStudio does all that for you when you just reference another class etc. Try it by removing the import and simply typing AlertDialog somewhere. Then select it from the list of suggestions in the dropdown.

The one you are probably looking for is import androidx.appcompat.app.AlertDialog.

Also, in Kotlin the type stands after the variable name (and is optional in that case):

val builder: AlertDialog = AlertDialog.Builder(context)

CodePudding user response:

import android.app.AlertDialog

Old AlertDialog class, shouldn't be used anymore. Still there for retrocompatibility purposes.

import android.support.v7.app.AlertDialog

AlertDialog that works on both old and new Android versions while supporting the new API. This requires a new dependency on the support v7 library.

import androidx.appcompat.app.AlertDialog

Android X (aka new SDK) AlertDialog. This is the one you want to use.

More on this

  • Related