0

I have met a problem when I try to use the TextInputLayout and TextInputEditText. Here is my code

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SignUpActivity">


    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:hint="Enter Password"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:inputType="textPassword"
            android:lines="1"
            android:hint="Enter Passwrod"/>
    </com.google.android.material.textfield.TextInputLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

And that's the screen shown in the design page Design Error Page

There should be something shown in the design page. I have check several youtube tutorial videos and those cant help me solve the problems.

Here is my build.gradle (module)

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    namespace 'com.james.kfarm'
    compileSdk 33

    defaultConfig {
        applicationId "com.james.kfarm"
        minSdk 24
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

    //Splash Screen API
    implementation 'androidx.core:core-splashscreen:1.0.0-beta02'


}

I have read several questions about the problems of using textinputlayout and textinputedittext. Some answers suggest that upgrading the version of "androidx.appcompat:appcompat" and "com.google.android.material:material". However I have upgraded them into the newest version and it dont work.

I have also rebuilt the project as the question "Android Studio cannot show design layout after adding a view" suggest

It would be a great help and comfort to me if anyone could help solve the problems. Thanks for reading this long question.

1 Answer 1

0

I have found how to solve the problems after trying several times. In fact, the theme that you used for the activity are not the one required. In my case, message suggest that the activity require "AppCompat" to instantiate the textInputLayout and the textInputEditText. And I have added add a new style

The below code is the theme that I used for the activity

    <style name = "Theme.App.Fullscreen" parent= "Theme.AppCompat.DayNight.NoActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>

I have navigated to the AndroidManifest.xml and change the theme for the related activity

<activity
     android:name=".SignUpActivity"
     android:exported="false"
     android:theme="@style/Theme.App.Fullscreen">
     <meta-data
         android:name="android.app.lib_name"
         android:value="" />
</activity>

After changing all the related code, I have closed the Android Studio and reopened it. And it finally show the textinputlayout and the textinputEdittext.

Success

However, when you want to change the style of the textinputlayout, like outlined box, you may need to change the theme from AppCompat to MaterialComponent to avoid the instantiation error

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.