I wanted to create a new Android launch screen for my Flutter app using constraint layout.
Firstly, I wasn't able to directly open layout builder in Android Studio in my Flutter app project. So using Android Studio, I created a new Android app with an empty activity and used the layout builder to create my layout by setting constraints. I then copied the generated XML into launch_background.xml of my Flutter project - where my previous splashscreen (not using constraint layout) had been working. I added this vectorDrawables.useSupportLibrary = true in the android.defaultConfig in and compile 'com.android.support:appcompat-v7:24.1.1' in dependencies, both in build.gradle.
I'm now unable to run my app because of errors for each constraint:
attribute layout_constraintTop_toTopOf (aka pha.myapp.dep.us:layout_constraintTop_toTopOf) not found.
...and repeat for each constraint.
How have I already tried to fix it?
I tried with this: implementation 'androidx.constraintlayout:constraintlayout:2.0.4' in the
dependencies in build.gradle but my app was stuck building for over
an hour.
I've run flutter clean
I've deleted my entire gradle file.
Am I missing something?
Finally, as reference, this is my XML file. The three drawables exist and have worked before when I haven't used constraint layout.
<?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=".MainActivity"
>
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="#drawable/ic_bg_splash" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="157dp"
android:layout_marginLeft="157dp"
android:layout_marginEnd="157dp"
android:layout_marginRight="157dp"
android:layout_marginBottom="32dp"
app:layout_constraintBottom_toBottomOf="#+id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:srcCompat="#drawable/ic_logo" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="56dp"
android:layout_marginLeft="56dp"
android:layout_marginEnd="56dp"
android:layout_marginRight="56dp"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toBottomOf="#+id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/imageView"
app:srcCompat="#drawable/ic_brandlogotag" />
</androidx.constraintlayout.widget.ConstraintLayout>
I created a fragment_b.xml file in my resources/layout folder. On my two buttons I get the following warning:
Element Button is not allowed here
Why is this warning popping up? My code is working fine.
fragment_b.xml:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<Button
android:id="#+id/btn_fragment_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/go_to_fragment_c"
app:layout_constraintBottom_toTopOf="#id/btn_nagivate_to_fragment_f"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<Button
android:id="#+id/btn_nagivate_to_fragment_f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/go_to_fragment_f"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/btn_fragment_b"
android:layout_marginTop="#dimen/a_lot_of_margin"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
This will happen if you don't have the androidx ConstraintLayout library added as a dependency in your gradle files.
In your app/build.gradle file, in the dependencies block, add this:
dependencies {
// ...
implementation "androidx.constraintlayout:constraintlayout:2.0.0-beta3"
}
Of course you can use a different library version, but the androidx.constraintlayout:constraintlayout: part is important.
Once that's done, do a gradle sync and rebuild your project and the warning should go away.
Try change it to:
androidx.appcompat.widget.AppCompatButton
or
com.google.android.material.button.MaterialButton
when first defining id ( #+id ) in constraint part ( such as app:layout_constraintBottom_toTopOf ) i get the error "cannot resolve symbol"
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#+id/tv2" />
<TextView
android:id="#id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
see the screen shot:
In gradle I have constraint layout 1.1.3 and Android studio version is 3.2.1 I recently updated it from older version:
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
app runs correctly without any error but the error is shown in android studio layout window.
Invalidating caches, rebuilding, and changing constraint layout version NONE of them helps!
In Java class R.id.tv2 works correctly and when i control+click it I can see its field in R class. editor is using another class for ids that is not same as the R class in java code?
This question was about a bug in old Android Studio and ConstraintLayout versions in current versions of Android Studio using androidx library this bug is not presented.
The plus sign + is added when defining the id (#+id/tv2) but when referencing you don't need to add the plus sign like this #id/tv2.
Instead of this
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#+id/tv2" />
<TextView
android:id="#id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
use this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#id/tv2" />
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
To get it right I suggest you convert your code to Androidx.
The below code is written in Androidx
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#id/tv2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Screenshot
As already mentioned in the other answers, use #+id/<your_name> to declare new resources.
In my case, Android Studio complained when using the - character in names (even though the app compiled and worked either way).
This question was about a bug in old Android Studio and ConstraintLayout versions in current versions of Android Studio using androidx library this bug is not presented.
As shown in the image, the constraints are not visible besides activating them.
I have no idea how to solve this problem.
Thanks.
[Edit]
Here is the XML Code, i haven't changed anything just added the Elements on the Design Tab.
<?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"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="81dp">
<Button
android:id="#+id/btn_jump"
android:layout_width="128dp"
android:layout_height="48dp"
android:text="Jump Activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
<EditText
android:id="#+id/edt_Name"
android:layout_width="215dp"
android:layout_height="43dp"
android:layout_marginTop="107dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
</android.support.constraint.ConstraintLayout>
It is happen In Android Studio 3.1.3 when We are using appcompact-v7:28.0.0-alpha3 library (It automatically take this library). Open the build.gradle (Module:app) and check in the dependencies that which version of appcompact are you using. If "com.android.support:appcompat-v7:28.0.0-alpha3" then just changed the alpha3 to alpha1 or you can use the previous version 27 also. Now you can see it all component in the blueprint.
Well this is a known issue in appCompat library version v7-28.0.0alpha/ which is being used directly with the latest build tools!
there are two solutions to this!
either upgrade your build tools from your sdk manager's tools tab!
or second way
is reverting back to 27.1.1
change the highlighted one into as
My version version of Android Studio (v3.1.4) has a slightly different line of code for appcompact with no mention of alpha3
implementation 'com.android.support:appcompact-v7:28.0.0-rc01' so i just changed it to
above version 27 and clicked Sync Project With Gradle Files and it now shows the constraints perfectly.
Had a crash while trying to use the new TextInputField for Android and wanted to share my solution.
Trying the new TextInputField in the android appcompat library was crashing my app. Here was my layout xml.
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="e-mail"
android:inputType="textEmailAddress"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
The error I got:
android.view.InflateException: Binary XML file line #20: Error inflating class android.support.design.widget.TextInputLayout.
SOLUTION:
Add the hintTextAppearance attribute to your TextInputLayout, so the lead tag looks like this:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="#android:style/TextAppearance.Medium">
Make sure you have the following dependencies in your gradle file:
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'
Working example:
<android.support.design.widget.TextInputLayout
android:id="#+id/txtEmail_InpLyt"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/txtEmail"
android:hint="Email Address"
android:singleLine="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</android.support.design.widget.TextInputLayout>
(Setting hintTextAppearance is not necessary.)
Update:
If you experience issues with the hint text not appearing in newer versions of Android (Marshmallow / Nougat), update library to version 22.2.1 (see TextInputLayout not showing EditText hint before user focus on it).
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:design:22.2.1'
This happened to me as well, and I came up with a solution that does not require changing the App Theme, but merely changing the Theme of the TextInputLayout:
<android.support.design.widget.TextInputLayout
android:id="#+id/testingInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Theme.AppCompat">
<EditText
android:id="#+id/testingEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/testText"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
You will need to add the appCompat library if you have not already:
compile 'com.android.support:appcompat-v7:23.0.1'
in adroidx it worked for me
i have the libraries
implementation 'com.android.support:appcompat-v7:28.0.0'
//design widget
implementation 'com.android.support:design:28.0.0'
i change
<android.support.design.widget.TextInputLayout
for
<com.google.android.material.textfield.TextInputLayout
I got the same issue when inflating XML containing TextInputLayout.
The problem was fixed by setting the correct Style on my application. Just like it says here : android design support library
I've the following issue
Caused by: android.view.InflateException: Binary XML file line #38: Error inflating class android.support.design.widget.TextInputLayout
My style.xml was
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="android:Theme.Material">
<!-- Customize your theme here. -->
</style>
</resources>
As it said in this post on Design Support Library
Note that as the Design library depends on the Support v4 and AppCompat Support Libraries, those will be included automatically when you add the Design library dependency.
So NO NEED TO ADD the following line inside the gradle file
compile 'com.android.support:appcompat-v7:22.2.0'
I found the link behove explaining that the Design Support Library is part of the AppCompat and it require the AppCompat Theme base to work. So
I've modify my style.xml to be
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
And it worked.
just to be clear as of Android Studio 3.* this is now changed to
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
(if this goes red a squiggly when you add it accept the version Android Studio wants to suggest)
this needs to be inside the
dependencies {}
section of build.gradle (Module:app) so it looks like this
dependencies {
...
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
}
then hit sync now
Use the following:
View view = LayoutInflator.from(getActivity()).inflate(R.layout.your_layout_file,parent,false);
Make sure the LayoutInflater is created from an AppCompatActivity
ex.
instead of
inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
use:
inflater = LayoutInflater.from(<appCompatActivity>);
None of the solution worked for me. I am using
compile 'com.android.support:appcompat-v7:28.0.0'
compile 'com.android.support:design:28.0.0'
Tried all the soluton. My app runs perfectly in devices with lower sdk. But crashes in newer( greater than sdk 26) devices. But after scratching head for 2 days finally got the solution.
my layout editor showed the error of
The following classes could not be instantiated:
- android.support.design.widget.TextInputLayout
But it did not gave me enough information to work with. It was not until I looked at other errors at the layout editor. Under render problem I found that
Couldn't resolve resource #string/path_password_strike_through
I finally got the solution after investigating this render problem. All you need to do is add
app:passwordToggleDrawable="#string/string_name"
in your TextInputLayout xml file.
<android.support.design.widget.TextInputLayout
android:id="#+id/login_input_layout_password"
android:layout_width="match_parent"
app:passwordToggleDrawable="#drawable/ic_lock_outline_grey600_24dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:backgroundTint="#color/colorAccent"
app:boxStrokeColor="#555"
android:textColorHint="#color/colorPrimary">
<EditText
android:id="#+id/login_input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</android.support.design.widget.TextInputLayout>
Finally I got this working...as per as the latest changes in the library itself, change the library from
<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt_input"
android:layout_margin="#dimen/activity_horizontal_margin"
app:layout_constraintTop_toBottomOf="#+id/edt_first_name"
app:layout_constraintStart_toEndOf="#+id/guideline"
app:hintEnabled="false">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Floating Hint Disabled" />
</android.support.design.widget.TextInputLayout>
to
<com.google.android.material.textfield.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt_input"
android:layout_marginTop="#dimen/activity_horizontal_margin"
app:layout_constraintTop_toBottomOf="#+id/edt_first_name"
app:layout_constraintStart_toEndOf="#+id/guideline"
app:hintEnabled="false">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Floating Hint Disabled" />
</com.google.android.material.textfield.TextInputLayout>
you might have added it
compile 'com.android.support:appcompat-v7:22.2.0' but you need to add compile 'com.android.support:design:22.2.0'
you can try this dependencies:
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'
in your gradle file.
I had the same issue with Android Studio 3.0.1
All you need to add the following line into gradle.properties (Project Propeties):
android.enableAapt2=false
for me work changes from EditText to TextInputEditText
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/date_raffle"/>
</android.support.design.widget.TextInputLayout>
Using implementation 'com.google.android.material:material:1.1.0' all you have to do is use your XML tags like this: <com.google.android.material.textfield.TextInputLayout for it to work...In my case it applies to my <com.google.android.material.textfield.TextInputEditText as well. Happy coding!...