How to make Constraint Layout Guideline in center

Yeahia2508 picture Yeahia2508 · Nov 4, 2017 · Viewed 12.3k times · Source

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>

Answer

Manav Jain picture Manav Jain · Nov 4, 2017

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