Search code examples
android-jetpack-composetextfieldandroid-jetpack

Clicking on TextField while it has focus does not show keyboard in jetpack compose Android


I am creating an android application jetpack compose. I implemented a TextField like this :

fun NameViewer(nameState: MutableState<String>) {

    Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.padding(bottom = 8.dp)) {
        Text(text = "Item Name:", modifier = Modifier.width(104.dp))
        TextField(
            value = nameState.value,
            onValueChange = { if (it.length <= 15) nameState.value = it },
            placeholder = { Text("Enter name") },
            modifier = Modifier.weight(1f),
            singleLine = true
        )
    }
}

When I click on the TextField first time, keyboard shows up as expected, then if I press back button the keyboard hides as expected, but if I click on the TextField again, the keyboard doesn't show up. It only shows up if I click on another TextField and then click on the previous TextField. Same behavior is shown by all the TextFields in the activity. Here is the full code of the activity

Update I did some more testing and now Now I am absolutely certain that the problem is is some incompatibility in dependencies. So here are my gradle files

Module level

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
    id("com.google.devtools.ksp")
}

android {
    namespace = "com.example.wastesamaritanassignment1"
    compileSdk = 34

    defaultConfig {
        applicationId = "com.example.wastesamaritanassignment1"
        minSdk = 24
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true
        }
    }

    buildTypes {
        release {
            isMinifyEnabled = true
            isShrinkResources = true
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = "17"
    }
    buildFeatures {
        compose = true
        buildConfig = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.5.4"
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
}

dependencies {
    implementation(platform("androidx.compose:compose-bom:2023.10.01"))
    androidTestImplementation(platform("androidx.compose:compose-bom:2023.10.01"))

    val roomVersion = "2.6.1"
    implementation("androidx.room:room-runtime:$roomVersion")
    annotationProcessor("androidx.room:room-compiler:$roomVersion")
    ksp("androidx.room:room-compiler:$roomVersion")
    implementation("androidx.room:room-ktx:$roomVersion")
    implementation("androidx.room:room-paging:$roomVersion")

    implementation("io.coil-kt:coil-compose:2.5.0")

    implementation("androidx.core:core-ktx:1.12.0")
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")

    implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.6.2")
    implementation("androidx.compose.runtime:runtime-livedata:1.5.4")

    implementation("androidx.paging:paging-compose:3.2.1")

    implementation("androidx.activity:activity-compose:1.8.2")
    implementation(platform("androidx.compose:compose-bom:2023.10.01"))
    implementation("androidx.compose.ui:ui")
    implementation("androidx.compose.ui:ui-graphics")
    implementation("androidx.compose.ui:ui-tooling-preview")
    implementation("androidx.compose.material3:material3:1.2.0-beta01")

    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
    androidTestImplementation(platform("androidx.compose:compose-bom:2023.10.01"))
    androidTestImplementation("androidx.compose.ui:ui-test-junit4")
    debugImplementation("androidx.compose.ui:ui-tooling")
    debugImplementation("androidx.compose.ui:ui-test-manifest")
}

Top level:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id("com.android.application") version "8.2.0" apply false
    id("org.jetbrains.kotlin.android") version "1.9.20" apply false
    id("com.google.devtools.ksp") version "1.9.20-1.0.13" apply false
}

Can someone please tell me how i can fix this?


Solution

  • Ok I found the the problem. It was the beta material 3 library of jetpack compose that was causing the issue.

    implementation("androidx.compose.material3:material3:1.2.0-beta01")
    

    So I changed it to

    implementation("androidx.compose.material3:material3:1.1.2")
    

    which solved the issue.