1

I'm trying to manipulate views inside a dialog, but the only way I can retrieve the views is in the Java old fashioned way, like this:

 class MyDialog: DialogFragment() {
        override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
            val alert = AlertDialog.Builder(context!!)
            val inflater = LayoutInflater.from(activity)
            val view = inflater?.inflate(R.layout.pg_dialog, null)
            with(alert) {
                setView(view)
                setNeutralButton(R.string.scambio_back, DialogInterface.OnClickListener({ _: DialogInterface, _: Int -> dialog.dismiss() }))
            }
            view?.findViewById<TextView>(R.id.dialog_name)?.text = person.name
            view?.findViewById<TextView>(R.id.dialog_surname)?.text = person.surname

            return alert.create()
        }
    }

Do you know any way to retrieve inner views in Kotlin, avoiding findViewById?

[Edit] Here's the build.gradle file:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 27
defaultConfig {
    applicationId "com.project.persons"
    minSdkVersion 21
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.0.2'
    implementation 'com.android.support:recyclerview-v7:27.0.2'
    implementation 'com.android.support:cardview-v7:27.0.2'
    implementation 'com.android.support:design:27.0.2'
    implementation 'com.android.support:support-v4:27.0.2'

    // Firebase
    implementation 'com.google.firebase:firebase-auth:11.8.0'
    implementation 'com.google.firebase:firebase-database:11.8.0'

    implementation 'com.firebaseui:firebase-ui-auth:3.2.2'

    implementation 'com.facebook.android:facebook-android-sdk:4.30.0'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
}

apply plugin: 'com.google.gms.google-services'
marcolav
  • 405
  • 1
  • 6
  • 17

2 Answers2

3

In this case, when you're not in a Fragment or an Activity, but have a View reference that you want to search for children with a given ID, you can use Kotlin Android Extensions with this syntax:

view.dialog_name.text = person.name
view.dialog_surname.text = person.surname

As with always when using Extensions, make sure you have the correct imports.

zsmb13
  • 85,752
  • 11
  • 221
  • 226
  • I do use Extensions, but view.dialog_name cannot be found, it gives me an unresolved reference over dialog_name, though importing kotlinx.android.synthetic.main.pg_dialog.* – marcolav Mar 16 '18 at 07:54
  • Can you post your `build.gradle` file? Perhaps it's not configured correctly. – zsmb13 Mar 16 '18 at 11:03
  • Thanks for the answer. Just illustrating a little bit more with an example on how to get the view: ```val alertDialog = alertDialogBuilderUserInput.create() alertDialog.show() alertDialog.etName.setText("a")``` – Francislainy Campos Mar 18 '19 at 06:57
-1

Since your question is a little bit unclear I'd say you refer to do something like this :

Do import apply plugin: 'kotlin-android-extensions in your build.gradle

Then you have to import this in your Activity

import kotlinx.android.synthetic.main.activity_pg_dialog.*

If you for example have a TextView with an id test you could do this:

test.setText("test text!!")

References :

Documentation Kotlin Android Extensions

Example Kotlin Android Extendions

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • Your solution does compile, but dialog_name is null and throws IllegalStateException. I imported kotlinx.android.synthetic.main.pg_dialog.* and the line throwing exception is dialog_name.text = person.name – marcolav Mar 16 '18 at 07:23