Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve class android.support.constraint.ConstraintLayout

I am getting this line in red color

<android.support.constraint.ConstraintLayout 

and app crashes on opening. it was working ok but now it gets an error: cannot resolve class android.support.constraint.constraintlayout

I also tried adding dependencies into the module Gradle file and also try fixing with suggested fixes by (add dependencies on androidx.constraints..,) link, but still no luck. I am new to this. Please help.

Here is the all code of main activity:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    android:background="@drawable/background"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/act1Btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:text="Bill Calculation"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/askOptions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:text="What you want to do?"
        android:textColor="#00BCD4"
        android:textSize="30sp"
        app:layout_constraintBottom_toTopOf="@+id/act1Btn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="130dp"
        android:layout_height="55dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:text="Tax Calc"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/act1Btn" />

    <Button
        android:id="@+id/btn4"
        android:layout_width="142dp"
        android:layout_height="46dp"
        android:text="Mobile Tax"
        app:layout_constraintBottom_toTopOf="@+id/btn3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/act1Btn" />
</android.support.constraint.ConstraintLayout>
like image 431
Asim Avatar asked Dec 23 '22 16:12

Asim


2 Answers

  1. Ensure you have the maven.google.com repository declared in build.gradle (Project: Name) file:
repositories {
    google()
}
  1. Add the library as a dependency in build.gradle (Module: app) file:
dependencies {
    implementation "androidx.constraintlayout:constraintlayout:2.1.0"
}
  1. To use ConstrainLayout write:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--    Your Content Here    -->
    
</androidx.constraintlayout.widget.ConstraintLayout>
like image 124
iknow Avatar answered Jan 17 '23 12:01

iknow


In my case I pasted code from a tutorial, which instead of

<?xml version="1.0" encoding="utf-8"?>
   <ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

(as it was in the tut) or even

<android.support.constraint.ConstraintLayout
. . .

it should have been:

<androidx.constraintlayout.widget.ConstraintLayout
. . .

Hope this helps someone!

like image 20
kwishnu Avatar answered Jan 17 '23 13:01

kwishnu