Search code examples
androidxmlandroid-studioandroid-constraintlayout

GuideLine relative to another object


You need to make GuideLine relative to another object, and not relative to the screen. While I am using this:

app:layout_constraintGuide_percent="0.875"

Solution

  • layout_constraintGuide_percent is always a percentage of the parent ConstraintLayout's height or width. You can continue to use a GuideLine, but you will have to manipulate the percentage programmatically either directly or through a facility like Data Binding.

    You can set the placement of a widget at 87.5% of the width (or height) of a widget with the use of another widget. Here is how you can do that:

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <View
            android:id="@+id/viewWeNeedPercentageOf"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:background="@android:color/holo_blue_light"
            android:src="@drawable/ic_launcher_foreground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <Space
            android:id="@+id/space85_7"
            android:layout_width="1px"
            android:layout_height="5dp"
            android:background="@android:color/holo_orange_light"
            android:visibility="gone"
            app:layout_constraintEnd_toEndOf="@id/viewWeNeedPercentageOf"
            app:layout_constraintHorizontal_bias="0.857"
            app:layout_constraintStart_toStartOf="@id/viewWeNeedPercentageOf"
            app:layout_constraintTop_toTopOf="@id/viewWeNeedPercentageOf" />
    
        <View
            android:id="@+id/viewToBePositioned"
            android:layout_width="10dp"
            android:layout_height="0dp"
            android:background="@android:color/holo_red_light"
            app:layout_constraintBottom_toBottomOf="@id/viewWeNeedPercentageOf"
            app:layout_constraintStart_toEndOf="@id/space85_7"
            app:layout_constraintTop_toTopOf="@id/viewWeNeedPercentageOf" />
    
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    viewWeNeedPercentageOf is just the view we need to locate the viewToBePositioned within at 85.7% of its width.

    space85_7 is a Space widget that we will place at 85.7% of the width of viewWeNeedPercentageOf. We do this by setting its start and end constraints to the start and end of viewWeNeedPercentageOf and changing its horizontal bias to 0.875. The visibility is set to gone so it has zero width and height.

    We can now constrain the start side of viewToBePositioned to the space widget so it will now take the position that is 85.7% of the width of viewWeNeedPercentageOf

    enter image description here