Search code examples
android-constraintlayout

How can i center a view in a guideline?


Suppose i have a horizontal GuideLine at 30% of parent and a View (suppose a Button) how can i make that view be centered on the guideline?

Like this:

enter image description here

Update:

The view i'm using here has height based on ratio, and soltuion on answers doesnt works.

Here is current layout:

<?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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imgLogo"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="@id/guideline_30"
        app:layout_constraintDimensionRatio="H,1:1"
        app:layout_constraintEnd_toStartOf="@+id/guidelineV_75"
        app:layout_constraintStart_toEndOf="@+id/guidelineV_25"
        app:layout_constraintTop_toTopOf="@+id/guideline_30"
        app:srcCompat="@drawable/ic_launcher_background" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline_30"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.3" />

    <android.support.constraint.Guideline
        android:id="@+id/guidelineV_25"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.25" />

    <android.support.constraint.Guideline
        android:id="@+id/guidelineV_75"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.75" />

</android.support.constraint.ConstraintLayout>

Solution

  • Using fix width or height

    Need to use same Guideline for both top and bottom constraint like:

    app:layout_constraintTop_toTopOf="@id/guideline_30"
    app:layout_constraintBottom_toBottomOf="@id/guideline_30"
    

    xml:

    <android.support.constraint.ConstraintLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:id="@+id/imgLogo"
            android:layout_width="150dp"
            android:layout_height="150dp"
            app:layout_constraintTop_toTopOf="@id/guideline_30"
            app:layout_constraintBottom_toBottomOf="@id/guideline_30"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:src="@drawable/splash_logo" />
    
        <android.support.constraint.Guideline
            android:id="@+id/guideline_30"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.30" />
    
    </android.support.constraint.ConstraintLayout>
    

    output:

    enter image description here

    Using width and height based on the ratio

    • As you are using both width and height based on ratio so that it's not centered.
    • But to get your desired result you can also set Guideline = 0.15 (0.3 / 2) and modify constraintTop and constraintBotttom

    xml:

    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
        android:id="@+id/imgLogo"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="H,1:1"
        app:layout_constraintEnd_toStartOf="@+id/guidelineV_75"
        app:layout_constraintStart_toEndOf="@+id/guidelineV_25"
        app:layout_constraintTop_toTopOf="@+id/guideline_15"
        app:srcCompat="@drawable/ic_launcher_background" />
    
    <android.support.constraint.Guideline
        android:id="@+id/guideline_15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.15" />
    
    <android.support.constraint.Guideline
        android:id="@+id/guidelineV_25"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.25" />
    
    <android.support.constraint.Guideline
        android:id="@+id/guidelineV_75"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.75" />
    
    </android.support.constraint.ConstraintLayout>
    

    output:

    enter image description here