Search code examples
javaandroidmaterial-designmaterial-componentsmaterial-components-android

Missing components in material google library on Android


I am trying to use SwitchMaterial component within an Android project, however Android studio cannot find it at design time, and including it within my xml layouts causes a ClassNotFoundException at runtime.

  • Recent Android studio project so is already setup using AndroidX
  • 'com.google.android.material:material:1.0.0' is in my dependencies and gradle syncs with no errors
  • Some other Material components are present, eg I am using MaterialButton on the same XML/Activity
  • Unable to find com.google.android.material.switchmaterial.SwitchMaterial in xml
  • Unable to import com.google.android.material.switchmaterial.SwitchMaterial when trying to add to activity java unit
  • Build settings : Compile SDK = 29 Build Tools = 29.0.2 Min SDK = 19 Target SDK = 29
  • There are no proguard rules setup (At all) so should not be messing with it
  • Spent an hour googling answers - nothing on SO, or the Android / Material Docs

Cannot find SwitchMaterial even though other components are there Missing Components

app build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "my.app.id"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "0.0.1"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    dexOptions {
        javaMaxHeapSize "6g"
    }
    lintOptions {
        lintConfig file("lint.xml")
        showAll false
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.github.bumptech.glide:glide:4.10.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'com.google.code.gson:gson:2.8.6'
}

XML layout


        <com.google.android.material.switchmaterial.SwitchMaterial
            android:id="@+id/login_notificationsSwitchFamily"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/margin_10"
            android:checked="true"
            android:text="Family Friendly"
            android:textSize="@dimen/textSize_16" />

Any help is appreciated. I cannot see why there would be missing components.


Solution

  • You need the version 1.1.0 of the material components library.

    Currently

     implementation 'com.google.android.material:material:1.1.0'