2

Exactly as the title suggests when I have a floating action button anywhere in my layout in any xml file the preview stops working, I can literally comment it out and it will instantly redraw perfectly. any ideas?

I have tried resetting multiple times syncing, invalidating caches etc. When i reset the view is rendered fine but only for pages open any i open after will not render and navigating back to the previously rendered layout breaks it

The only thing I can think of is I use clans floating action button in my project, but it's the Google one that breaks my preview.

EDIT I've removed clans cleaned project invalidated caches restarted and still the issue persists

also if i change FloatingActionButton to ExtendedFloatingActionButton the preview draws fine, looking into those two it seems they extend completely different classes

Also using AndroidX

here is an example of an xml that will not render

<?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=".settings.PdfSettingsActivity">

<ImageView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  app:layout_constraintBottom_toBottomOf="parent"
  app:layout_constraintStart_toStartOf="parent"
  app:layout_constraintEnd_toEndOf="parent"
  app:layout_constraintTop_toTopOf="parent"
  android:scaleType="centerCrop"
  android:layout_margin="@dimen/activity_vertical_margin_16dp"
  android:elevation="@dimen/activity_horizontal_margin_4dp"
  android:id="@+id/pdf_renderer"/>




<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/share"
    android:layout_margin="@dimen/activity_vertical_margin_16dp"
    android:src="@drawable/ic_share_white_24dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

if i comment out the floating action button the view immediately renders

com.google.android.material.floatingactionbutton.FloatingActionButton

Android Studio 3.4.1 Build #AI-183.6156.11.34.5522156, built on May 1, 2019 JRE: 1.8.0_152-release-1343-b01 x86_64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o macOS 10.14.3

EDIT

Just playing around with it now and I manage to make it throw an an error:

java.lang.IllegalArgumentException: java.lang.ClassCastException@54291cbb
at sun.reflect.GeneratedMethodAccessor971.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at android.animation.PropertyValuesHolder_Delegate.callMethod(PropertyValuesHolder_Delegate.java:108)
at android.animation.PropertyValuesHolder_Delegate.nCallFloatMethod(PropertyValuesHolder_Delegate.java:143)
at android.animation.PropertyValuesHolder.nCallFloatMethod(PropertyValuesHolder.java)
at android.animation.PropertyValuesHolder.access$400(PropertyValuesHolder.java:38)
at android.animation.PropertyValuesHolder$FloatPropertyValuesHolder.setAnimatedValue(PropertyValuesHolder.java:1387)
at android.animation.ObjectAnimator.animateValue(ObjectAnimator.java:990)
at android.animation.ValueAnimator.animateBasedOnTime(ValueAnimator.java:1339)
at android.animation.ValueAnimator.doAnimationFrame(ValueAnimator.java:1471)
at android.animation.ValueAnimator.pulseAnimationFrame(ValueAnimator.java:1490)
at android.animation.AnimatorSet.pulseFrame(AnimatorSet.java:1163)
at android.animation.AnimatorSet.handleAnimationEvents(AnimatorSet.java:1146)
at android.animation.AnimatorSet.doAnimationFrame(AnimatorSet.java:1046)
at android.animation.AnimationHandler.doAnimationFrame(AnimationHandler.java:146)
at android.animation.AnimationHandler.access$100(AnimationHandler.java:37)
at android.animation.AnimationHandler$1.doFrame(AnimationHandler.java:54)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:947)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer_Delegate.doFrame(Choreographer_Delegate.java:66)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.renderAndBuildResult(RenderSessionImpl.java:563)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.render(RenderSessionImpl.java:425)
at com.android.layoutlib.bridge.BridgeRenderSession.render(BridgeRenderSession.java:120)
at com.android.ide.common.rendering.api.RenderSession.render(RenderSession.java:151)
at com.android.ide.common.rendering.api.RenderSession.render(RenderSession.java:133)
at com.android.tools.idea.rendering.RenderTask.lambda$null$8(RenderTask.java:755)
at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)
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)

build.gradle dependencies

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
//Room dependencies
implementation 'androidx.room:room-runtime:2.1.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.firebase:firebase-core:16.0.4'
implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
implementation 'com.google.firebase:firebase-database:16.0.4'
implementation 'com.google.firebase:firebase-storage:16.0.4'
implementation 'com.google.firebase:firebase-auth:16.0.5'
implementation 'com.firebase:firebase-client-android:2.4.0'
annotationProcessor 'androidx.room:room-compiler:2.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0-alpha01'
annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.2.0-alpha01"
//GSON
implementation 'com.google.code.gson:gson:2.8.5'
//UI design
implementation 'com.github.hotchemi:android-rate:1.0.1'
implementation "androidx.legacy:legacy-support-core-utils:1.0.0"
implementation 'com.google.android.material:material:1.1.0-alpha07'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha06'
implementation 'com.github.siyamed:android-shape-imageview:0.9.+@aar'
implementation 'com.getkeepsafe.taptargetview:taptargetview:1.12.0'
implementation 'com.romandanylyk:pageindicatorview:1.0.3'
implementation 'com.github.VRGsoftUA:ParallaxView:1.0'
//Glide
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
//Edit image
implementation 'com.yashoid:instacropper:1.0.6'
// FloatingActionButton
implementation 'com.github.martipello:FloatingActionButton:v2.0'
//Navigation
implementation "androidx.navigation:navigation-fragment:2.1.0-alpha05"
// For Kotlin use navigation-fragment-ktx
implementation "androidx.navigation:navigation-ui:2.1.0-alpha05"
//material components
implementation 'androidx.appcompat:appcompat:1.1.0-beta01'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha4'
implementation 'androidx.vectordrawable:vectordrawable:1.1.0-beta02'
//tests
testImplementation 'org.json:json:20180130'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.2.0'
implementation 'androidx.test.espresso:espresso-idling-resource:3.2.0'
androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.2.0'
implementation 'org.apache.commons:commons-lang3:3.8.1'
}
martinseal1987
  • 1,862
  • 8
  • 44
  • 77
  • Can you provide your layout xml? – Wilson Sim Jun 26 '19 at 07:32
  • You can get some help with this - https://stackoverflow.com/a/24523238/5773044 – Anupam Jun 26 '19 at 07:32
  • try to refresh your layout – Ali Amini Jun 26 '19 at 07:43
  • @WilsonSim yes i can but i can assure you it wouldnt make a slight of difference i can make an empty xml, add any layout group, linear, constraint etc everything will be fine but the moment i add a floating action button the preview stops working, i can also add an entire layout with a floating action button, the layout wont render, if i remove the action button the layout preview redraws and is fine, and then adding the action button back the preview stays the same and will not update anymore – martinseal1987 Jun 26 '19 at 07:57
  • 1
    @AnupamBose thanks that can sometimes be an issue but not in this case, as already mentioned the preview is fine until i add the ation button – martinseal1987 Jun 26 '19 at 07:59
  • You probably saw the other answers there too, but sometimes you need to update your base style. In particular, if you have attributes in styles such as MaterialComponents, you require a preview with that style as well – Allan W Jun 26 '19 at 08:03
  • Perhaps also verify that your appcompat versions are consistent – Allan W Jun 26 '19 at 08:04
  • Do you have the right dependency for `Floating action button`? If you are using androidx, make sure the others library are androidx as well. – Wilson Sim Jun 26 '19 at 08:06
  • @AllanW i think this is the most likely but my base is currently Base.Theme.MaterialComponents.Light.DarkActionBar.Bridge – martinseal1987 Jun 26 '19 at 08:59
  • @WilsonSim dependencies is a good shout will lokk at that now – martinseal1987 Jun 26 '19 at 09:01
  • @WilsonSim dependencies look fine, updated to alpha07 but no joy – martinseal1987 Jun 26 '19 at 09:07
  • YAY updated all dependencies invalidated cache and restarted android studio and the preview now displays – martinseal1987 Jun 26 '19 at 09:30
  • take that back it just worked for the layout it opened on start up – martinseal1987 Jun 26 '19 at 09:35
  • why has this been down voted? – martinseal1987 Jun 27 '19 at 09:55

7 Answers7

4

Seems like an issue with Android Studio which should be fixed in 3.5 beta 2 (as well as the latest beta I imagine). More specifically, this seems to have been Issue#129926965.

Credit: here

1

When comparing to the documentation... you probably have to use attribute srcCompat instead:

<com.google.android.material.floatingactionbutton.FloatingActionButton
   android:id="@+id/share"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="bottom|end"
   android:layout_margin="16dp"
   app:srcCompat="@drawable/ic_share_white_24dp"/>

While this part of the build.gradle appears confusing / redundant:

implementation "com.github.martipello:FloatingActionButton:v2.0"
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
0

I've noticed that you are using AndroidX. Since you can't use support libraries with AndroidX remove all the support libraries in your build.gradle files and add this:

implementation 'com.google.android.material:material:1.1.0-alpha07'

To your app/build.gradle file.

EDIT:

If that didn't work, check for

android.enableJetifier=true
android.useAndroidX=true

in your gradle.properties file.

SnakeException
  • 1,148
  • 1
  • 8
  • 26
0

Have you tried messing with the elevation of the button?

android:elevation 

or

app:elevation
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
CCT
  • 70
  • 1
  • 6
0

It's a known bug/issue in Android Studio 3.4.x https://issuetracker.google.com/issues/132316448 You just have to wait for Android Studio 3.5. But if you also use the Beta versions then this issue seems to be rectified in beta 2 and above but not confirmed.

Vanshaj Daga
  • 2,145
  • 11
  • 18
0
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="16dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"

Try to write these lines manually in the Code section rather than in the design section. It worked for me

-2

I guess your cast in Activity or Fragment is written wrong.

Check you imported correct FloatingActionButton class. And it is correspond with your View in xml.

For example,

FloatingButton btnFab = (FloatingButton) findViewById(R.id.btn_fab) // Not FloatingActionButton

in your xml.

<!-- Not FloatingButton -->
<com.example.FloatingActionButton 
     android:id="@+id/btn_fab"
     android:layout_width = "45dp"
     android:layout_height = "45dp"
     />

Check if the class names are matched.

c-an
  • 3,543
  • 5
  • 35
  • 82