1

I'm having an issue where I'm trying to constrain a button to a vertical barrier. When I drag the button's side that I want to constrain to the barrier, it doesn't seem to recognise the barrier. It doesn't snap onto it or apply any constraint after hovering onto it and letting go.

I've tried replicating a simple tutorial from YouTube, where he constrains his button to the barrier with no issue at the time stamped link: https://youtu.be/Ngz5cgLC7m4?t=180.

I'm aware you can add the barrier ID to the Button's XML's left contraint. But it doesn't exactly fix the underlying issue.

I've also checked my version's current changelog on barriers (nothing changed).

edit: here's my 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:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="TextView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:text="TextView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView4" />

    <android.support.constraint.Barrier
        android:id="@+id/barrier3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="end"
        app:constraint_referenced_ids="textView5,textView4"
        tools:layout_editor_absoluteX="411dp" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        tools:layout_editor_absoluteX="173dp"
        tools:layout_editor_absoluteY="64dp" />
</android.support.constraint.ConstraintLayout>
MrLem
  • 19
  • 4
  • How do you really expect us to help in your code without your code? – Lalit Fauzdar Jun 05 '20 at 21:14
  • 1
    Posted it. Wasn't really expecting this to be a code issue. I just can't constrain a button to a barrier in the design/blueprint view. I can however, constrain a button to a guideline. – MrLem Jun 05 '20 at 21:31

1 Answers1

0

Some minor problems come with the Graphical design editor like you can't be 100% sure that it didn't use absoluteX and absoluteY instead of Constraints. I always prefer the text editor because you know the code as you can see the code and modify it as you want.

So, I checked your code and saw that you're using absoluteX and absoluteY instead of Constraints for the button. You can constrain it to the barrier as

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"

    //These two constraints will set the position on Horizontal axis 
    app:layout_constraintStart_toEndOf="@id/barrier3"
    app:layout_constraintEnd_toEndOf="parent"

    //You should replace it as well with constraints.
    tools:layout_editor_absoluteY="64dp" />

Now, as you can see I've replaced absoluteX with Start_toEndOf and End_toEndOf. Now, to set vertical constraints, I don't actually understand why is y's position 64dp, but you can do one thing. Either constrain it with Top and use it as marginTop from Top or You can use Vertical Bias.

You can replace absoluteY with this:

    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintVertical_bias="0.1"

Vertical_bias 0.1 will give you similar result, it will postition your button to 10% of the screen size and it's dynamic which means it will be on same position on any screen size.

Just Change your code with this and I hope you'll have your answer. You can ask anything else if needed.

Lalit Fauzdar
  • 5,953
  • 2
  • 26
  • 50
  • New to Android Studio. I think those absoluteX and absoluteY values were generated depending on where I drag and dropped the button in the design view. I hadn't applied any constraints to the button and was more focused on why the button's left constraint wouldn't snap/apply onto the barrier in design view. – MrLem Jun 06 '20 at 13:05
  • @MrLem Indeed, they get generated by moving any widget in the design editor. But, that's now how you should position your widgets, you should use `Constraint` for that. I've shown the way you should go with. – Lalit Fauzdar Jun 06 '20 at 13:20