Home > other >  How to set up floating action button expansion direction in Android Studio?
How to set up floating action button expansion direction in Android Studio?

Time:07-31

I try to give to a height of floating action button a set of constraints, that will look like "bottom of button is 8dp from bottom of screen, top of button is 92% of screen size from top of screen, but, if button height (according to previous constraints) become lower than 40dp, expand button in top direction to achieve this height". I've almost done this successfully, but, when button starts to expand to achieve 40dp height, it expands in both directions - top and bottom - and I can't restrict it expansion to only top direction. Are there any ways to do what I want to do? Also, I have more global question - are there any ways to give to one constraint higher priority, than to another?

Code of button:

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@ id/floatingActionButton"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:clickable="true"
    android:foreground="@drawable/ic_baseline_add_24"
    app:backgroundTint="@color/amtheme"
    app:layout_constraintBottom_toTopOf="@ id/guideline23"
    app:layout_constraintDimensionRatio="1:1"

    app:layout_constraintEnd_toStartOf="@ id/guideline9"
    app:layout_constraintHeight_min="40dp"
    app:layout_constraintTop_toTopOf="@ id/guideline13" />

Screenshot 1: Button is 8dp from bottom of screen, 92% from top of screen, everything is fine! https://i.stack.imgur.com/BFX0e.png

Screenshot 2: I change screen orientation, so button becomes too small, it adjusts to minimal height constraint - and expands in both directions instead of expanding in top direction, moving offscreen. https://i.stack.imgur.com/NaTYp.png

CodePudding user response:

I found answer myself. Adding app:layout_constraintVertical_bias="0.8" in code helped me, may be it will help someone else!

  • Related