I started using Android Studio 2.2 and ConstraintLayout today, but I'm having some issues with the rendering. When using Blueprint Mode everything appears fine. When using Design Mode, the objects are stuck at the top left corner all mashed together.
Android Studio warning: onMeasure error
java.lang.NoSuchMethodError: android.support.constraint.solver.widgets.ConstraintWidget.immediateConnect(Landroid/support/constraint/solver/widgets/ConstraintAnchor$Type;Landroid/support/constraint/solver/widgets/ConstraintWidget;Landroid/support/constraint/solver/widgets/ConstraintAnchor$Type;II)V
at android.support.constraint.ConstraintLayout.setChildrenConstraints(ConstraintLayout.java:517)
at android.support.constraint.ConstraintLayout.updateHierarchy(ConstraintLayout.java:433)
at android.support.constraint.ConstraintLayout.onMeasure_Original(ConstraintLayout.java:728)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java)
at android.view.View.measure(View.java:19731)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19731)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:19731)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.measureView(RenderSessionImpl.java:545)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:342)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:429)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:389)
at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:548)
at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:533)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:966)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:533)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$72(RenderTask.java:659)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
The dependency:
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha8'
Is this an unsolvable error in Android Studio or is there something that is solvable? SDK is updated.
EDIT
XML file:
<?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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="#drawable/about"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView6"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="16dp" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView33"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="#+id/imageView6"
android:layout_marginLeft="16dp" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView34"
app:layout_constraintRight_toRightOf="#+id/textView33"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/textView33" />
</android.support.constraint.ConstraintLayout>
as mentioned here
Constraint Layout Alpha8 release notes
Note: due to API change, you might have to restart Android Studio 2.2 after upgrading to alpha 8 for layout preview to work.
so From the menu bar, select file and choose invalidate caches / restart
I had that problem after update
implementation 'androidx.constraintlayout:constraintlayout:1.1.3
to
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
downgrade back to 1.1.3 resolve problem
Related
In my Android Studio my constraint widget does not work. Whenever I put in a value for the top margin the design doesn't change. for example, in the image you will see 84 in the constraint widget I want to change it to 20 but when I do nothing happens. How do I make this work?
Here is the xml code:
<?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">
<TextView
android:id="#+id/txtVw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/ALButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onBtnClick"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/edtxtName" />
<EditText
android:id="#+id/edtxtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="84dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/txtVw" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is the design image:
I have a similar problem here. looks like a bug in your version of Android Studio. Try using Android Studio Arctic Fox|2020.3.1 Patch 2
I am currently learning Android Studio and ran into an issue in the activity_main.xml, where i cannot view then buttons, text and etc. (See image Link).
Link to Andoird Studio screenshot
I am also getting following error:
The following classes could not be instantiated:
- androidx.constraintlayout.widget.ConstraintLayout (Open Class, Show Exception, Clear Cache)
Exception Details java.lang.ClassNotFoundException: androidx.constraintlayout.core.widgets.analyzer.BasicMeasure$Measurer Copy stack to clipboard
I did try to build the project and update dependencies without success.
The XML code is:
<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">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="#tools:sample/cities" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="393dp"
android:layout_marginRight="393dp"
android:layout_marginBottom="657dp"
android:text="#string/button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
And I have following implementation:
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
Instead of the added dependency for constraint layout use the below:
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
I'm trying to build my first app in Android Studio 4.1.2. I have created a new project (Empty project) with default "Hello world!" Main Activity.
When trying to validate the activity_main.xml I get the following errors:
Error:External resource http://schemas.android.com/apk/res/android is not registered C:\AndroidStudioProjects\MyApp\app\src\main\res\layout\activity_main.xml
Error:(7, 35) cvc-elt.1.a: Cannot find the declaration of element 'androidx.constraintlayout.widget.ConstraintLayout'.
Error:Premature end of file.
I have already tried the following:
Reinstall Android studio
Sync Project with Gradle Files
Invalidate Caches / Restart
build.gradle also has the line implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
I can run the application in emulator or actual device but apparently the validation error prevents me to refer to the layout elements from Kotlin code.
Any ideas how to fix this error?
Here's the activity_main.xml in code:
<?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:id="#+id/layoutOne"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/textMyText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
androidx.constraintlayout.widget.ConstraintLayout
Change this to
RelativeLayout
I want to include a layout of .axml file in another .axml using xamarin android but,
when i use <include> it doesn't show any thing. What is the equivalent to <include> of android in mono android just like the link below:
android reuse layouts
There is no difference between Xamarin.Android (Mono for Android) or native Android's XML files. The axml extension is purely to trigger the code completion in VS and Xamarin Studio. Android does not care what extension the files have.
So let me allow to show a sample showing both the <merge> and <include> tags:
progress_with_text.axml:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:text="Loading..."
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/lblLoading"
android:layout_marginLeft="180dp"
android:layout_marginTop="240dp"/>
<ProgressBar
android:id="#+id/progBar"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="130dp"
android:layout_marginTop="230dp"/>
</merge>
main.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include
layout="#layout/progress_with_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
This works perfectly fine!
Once more I need some help. Trial and error is not working so well. Can someone please look at my code? I want my listview to contain admob ads at the top of the page and I am struggling a bit this morning. Thanks in advance!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
>
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="xxxxxxxxxxxxxxx"
ads:loadAdOnCreate="true"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/txtPlaceName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:ellipsize="end"
android:singleLine="true"
android:text="saa"
android:textSize="25sp"
android:layout_below="#id/adView" />
<LinearLayout
android:id="#+id/layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imgType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/marker1" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtCityState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="city, st"
android:textSize="15sp" />
<TextView
android:id="#+id/txtDistance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="15sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RatingBar
android:id="#+id/average_ratingbar"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:numStars="5"
android:stepSize="1.0" />
<TextView
android:id="#+id/txtCommentsCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="15sp" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Short answer
The Banner ad needs 320dp width. Your base LinearLayout has android:padding="5dp" so you only have 310dp left, and the ad therefore doesn't fit on the screen.
For the future
Android has a logging tool called LogCat that keeps track of all messages that are logged. AdMob SDK provides logging messages that can be very helpful when debugging these issues. This is how I was able to solve your problem.
You can view the LogCat output using the command line by following the directions from the link provided above, or you can view the LogCat output in Eclipse by going to Window -> Show View -> LogCat. When requesting an ad, the SDK logged the following message:
Not enough space to show the ad! Wants: <320,50>, Has: <310,473>
From there it was pretty easy to tell that you didn't have the full 320 width, and sure enough your top level layout had 5dp padding on each side.
Unrelated Layout Tip
Fyi, the android:layout_alignParentTop and android:layout_below attributes only do anything inside of a RelativeLayout. In the layout you provided above, these attributes on the AdView and the TextView don't do anything.