Search code examples
androidandroid-layoutandroid-constraintlayout

How to make Constraint Layout Guideline in center


I have 3 Guideline in Constraint Layout. One is in left with 16dpleft margin. Second is in right with 16dp right margin. But I want another Guideline which will be in center. Suppose If I make this guideline center in nexus 5 from Android Stdio XML design panel then in nexus S it's not showing in center. How to solve this?

 <android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.blabla.ScrollingActivity">

    <android.support.constraint.Guideline
        android:id="@+id/guideline_end"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_end="16dp" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="16dp" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="180dp"/>
 </android.support.constraint.ConstraintLayout>

Solution

  • We can set a guideline using percentage by using the tag

    app:layout_constraintGuide_percent="0.5"

    where 0.5 (50%) being a float value between 0 to 1