Element Button is not allowed here - android

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

Related

I am getting "message: XML document structures must start and end within the same entity" in Android Studio

I am new to Android Studio. I started building an app. This is the code of my xml file. I can't figure out the problem! This is the exact error code that I am getting."ParseError at [row,col]:[60,1]
Message: XML document structures must start and end within the same entity."
<?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"
android:background="#121212"
tools:context=".MainActivity"
>
<ImageView
android:id="#+id/imageView"
android:layout_width="844dp"
android:layout_height="1090dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.988"
app:srcCompat="#drawable/up_notch_adobespark" />
<ImageButton
android:id="#+id/imageButton4"
android:layout_width="50dp"
android:layout_height="54dp"
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.005"
tools:srcCompat="#drawable/more_button" />
<ImageButton
android:id="#+id/imageButton7"
android:layout_width="59dp"
android:layout_height="66dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.997"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.004"
app:srcCompat="#drawable/help_icon" />
<TextView
android:id="#+id/textView"
android:layout_width="165dp"
android:layout_height="36dp"
android:fontFamily="#font/baloo_tamma"
android:text="Hi Josh."
android:textSize="22dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.065"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.158" />
just like broot explained … XML syntax is similar to HTML it has an opening and closing tag, so you need to close every single tag you open in which your case is the constraint layout . just add this line to the end of your xml file
</androidx.constraintlayout.widget.ConstraintLayout>
another advice would be very useful is to avoid using horizontal and vertical bias, I believe that padding(s) and margins(s) are more than enough to help you design the layout you want
and try also using for width and height wrap_content or match parent for detailed and correct measurement for the most phones that will be used. because if you changed the amuleter you are using you will notice that the measurement will be diffrient so as a best practice is to use wrap_content or match parent
Error message is pretty self-explanatory. You opened ConstraintLayout tag at the beginning of the document, but you never closed it. You need to add the following at the end:
</androidx.constraintlayout.widget.ConstraintLayout>

Creating an Android launchscreen xml in Flutter project using constraint layout - constraints not found

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>

Defining id ( #+id ) in constraint part of ConstraintLayout error

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.

Can't Set Progress Bar in Fresco

I am reading the documentation for Fresco, and it says I can create a progress bar for a SimpleDraweeView with the following attribute:
fresco:progressBarImage="#drawable/progress_bar"
or with this code (I modified the example to work in Kotlin and work with a draweeView inside of a recyclerview)
val hierarchy : GenericDraweeHierarchy = GenericDraweeHierarchyBuilder.newInstance(context.resources)
.setProgressBarImage(ProgressBarDrawable())
.build()
holder.draweeView.setHierarchy(hierarchy)
However, in my XML I am getting the "cannot resolve symbol" error in reference to the drawable.
Additionally, the .setHierarchy(hierarchy) is not recognized.
What's going on?
My build.gradle has the following dependencies (among others):
implementation 'com.facebook.fresco:fresco:1.9.0'
implementation 'com.facebook.fresco:animated-gif:1.9.0'
Also, here is the XML layout for the item column for my recyclerview:
<?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:fresco="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<com.facebook.drawee.view.SimpleDraweeView
android:id="#+id/itemImage"
android:layout_width="300dp"
android:layout_height="0dp"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.7"
app:layout_constraintLeft_toRightOf="parent"
app:layout_constraintRight_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
fresco:progressBarImage="#drawable/progress_bar"
/>
<ToggleButton
android:id="#+id/itemFavoriteButton"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="#drawable/ic_favorite"
android:textOff=""
android:textOn=""
android:text=""
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.3"
app:layout_constraintLeft_toRightOf="parent"
app:layout_constraintRight_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/itemImage" />
</android.support.constraint.ConstraintLayout>

No resource identifier found for attribute 'layout_constraintBottom' in package

I'm trying to build a simple android app in Android Studio 2.3.2, but I'm getting the following error for component I add to the app: "No resource identifier found for attribute 'layout_constraintBottom' in package 'com..."
I researched this problem and found that some people are getting a similar error. However, the answers are suggesting to add "compile 'com.android.support.constraint:constraint-layout:1.0.2'" to the gradle.build file. When I looked there, the line is already in place.
Here is a snippet of my code:
<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/main_menu"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.nmeneses.scorekeeper.MainMenu"
tools:showIn="#layout/activity_main_menu">
<Button
android:id="#+id/button3"
android:layout_width="128dp"
android:layout_height="79dp"
android:layout_marginBottom="50dp"
android:layout_marginLeft="39dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="Teams"
android:textSize="23dp"
app:layout_constraintBottom="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.066"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
Any help would be greatly appreciated. If you need more info, please let me know.
AFAIK there is no attribute layout_constraintBottom in ConstraintLayout. There is layout_constraintBottom_toTopOf and layout_constraintBottom_toBottomOf.
I suggest that you use the following line instead:
app:layout_constraintBottom_toBottomOf="parent"

Categories

Resources