user12398554
user12398554

Reputation:

Why is SwitchCompat not working on Android Studio?

Why is SwitchCompat not working and have this gray rectangle shape? I think I've tried almost everything but it's still don't want to work.

Gradle file:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'
}

xml code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".ustawienia">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"/>

<include layout="@layout/content_ustawienia" />

    <android.support.v7.widget.SwitchCompat
        android:id="@+id/switch_animacja"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"/>
    
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Upvotes: 4

Views: 3726

Answers (1)

Md. Asaduzzaman
Md. Asaduzzaman

Reputation: 15423

Instead of

android.support.v7.widget.SwitchCompat

use

androidx.appcompat.widget.SwitchCompat

Besides this you are mixing support library with AndroidX library. Try to remove support library as they are deprecated.

If you didn't migrate your project to AndroidX cleanly Then migrate it by selecting Refactor > Migrate to AndroidX from Android Studio menu bar and press Do Refactor.

Check official documents for further details.

Upvotes: 7

Related Questions