0

Problem statement

I am using com.google.android.material.button.MaterialButton inside androidx.constraintlayout.widget.ConstraintLayout where the constraint layout cannot deploy or detect the constraints of the material button.

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=".views.screens.VeritasFolderReadSelectionScreen">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/folderSelectToolbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:background="@color/startGrad"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/AppBarOverlay"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/folderSelectMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:text="Select folders you want PeerPots to read"
        android:textSize="16sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/folderSelectToolbar"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/folderRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        app:layout_constraintTop_toBottomOf="@id/folderSelectMessage"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <!-- Following code not working -->
    <com.google.android.material.button.MaterialButton
        android:id="@+id/folderViewButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Done"
        android:textSize="12sp"
        android:layout_marginBottom="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

ScreenShot

enter image description here

Material button not visible

UPDATED

The error thrown while rendering

java.lang.IllegalArgumentException: java.lang.ClassCastException@207d8929 at sun.reflect.GeneratedMethodAccessor226.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)

Saswata
  • 1,290
  • 2
  • 14
  • 28

2 Answers2

1

I have verified everything looks good and its working as well. As you can see in the attached image it's your above-mentioned code.

enter image description here

Please make sure you added the dependency into your gradle file. If yes, then there may be some issue in some more properly working code here which is overlap on the button. So you need to check in that code.

implementation 'com.google.android.material:material:1.0.0'

enter image description here

Sunny
  • 3,134
  • 1
  • 17
  • 31
  • Updated the full code and also already using implementation 'com.google.android.material:material:1.0.0' – Saswata Jun 26 '19 at 05:20
  • The answer is updated and it's also working fine. Have you run the code device or simulator? I hope it's working fine. – Sunny Jun 26 '19 at 05:28
  • Your answer was right but unfortunately the reason of error or the solution is different and happens in rare and specific cases – Saswata Jun 26 '19 at 05:58
  • add one more constraint in your recyclerview **app:layout_constraintBottom_toTopOf="@+id/folderViewButton"** so that your button will not hide behind recyclerview. For the crash, you are facing checkout link https://stackoverflow.com/questions/52111656/using-material-button-gives-classnotfound-runtime-error – Vijay Patole Jun 26 '19 at 11:49
  • @VijayPatole No need to add this because the button is in the last that means it will be always on the top of the layout. – Sunny Jun 26 '19 at 11:50
0

After testing the code in other computers the reasons of the error found are-

  1. System support lack - The system used for coding was not able to provide enough resources to render the views efficiently and for which the preview does not work.

  2. Android Studio efficiency - On low capacity systems Android Studio cannot manage dependencies well while rendering and thus throws the error java.lang.IllegalArgumentException: java.lang.ClassCastException

Solution

  1. Optimal solution - Use the recommended system configuration as per documentation.

  2. Ostrich solution - Run the app every-time a change is made on the UI to see how it really works without depending on the Preview

Saswata
  • 1,290
  • 2
  • 14
  • 28