Change Guideline percentage in constraint layout programmatically

AndroidAndroid Constraintlayout

Android Problem Overview


I have a guideline in constraint layout like this

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

Later I want to change the app:layout_constraintGuide_percent value to something else conditionally. How can I achieve this.

Android Solutions


Solution 1 - Android

There is two ways to do it:

> This method get a specific parameter from the ConstraintLayout and modify it.

Guideline guideLine = (Guideline) findViewById(R.id.your_guideline);
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) guideLine.getLayoutParams();
params.guidePercent = 0.45f; // 45% // range: 0 <-> 1
guideLine.setLayoutParams(params);

>This method consists in cloning the ConstraintLayout attributes, modifying them and then applying to the View. > >BUT it's very slow.

ConstraintLayout constraintLayout = (ConstraintLayout) findViewById(R.id.your_constraint_with_guideline);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);        
constraintSet.setGuidelinePercent(R.id.your_guideline, 0.07f); // 7% // range: 0 <-> 1
TransitionManager.beginDelayedTransition(constraintLayout);
constraintSet.applyTo(constraintLayout);

Creating a parallax effect

To test those methods I created a parallax animation using a GuideLine and a ScrollView on top.

On the AndroidManifest.xml, inside your Activity, add this: android:hardwareAccelerated="true".

     <activity 
        android:name=".MainActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:hardwareAccelerated="true">
     ...

MainActivity.java

Guideline guideTopInfo;
ConstraintLayout constraintLayout;
ConstraintLayout.LayoutParams params;
ConstraintSet constraintSet;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    guideTopInfo = (Guideline) findViewById(R.id.guideline2);
    constraintLayout = (ConstraintLayout) findViewById(R.id.constraint_root);
    params = (ConstraintLayout.LayoutParams) guideTopInfo.getLayoutParams();

    //constraintSet = new ConstraintSet();
    //constraintSet.clone(constraintLayout);

    final ScrollView scrollView = (ScrollView) findViewById(R.id.scroll_front);
    scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            float percentage = scrollView.getScrollY() * 0.0001f; // 0.001f faster // 0.00001f slower parallax animation

            Log.d("mLog", String.valueOf(percentage));
            if(percentage >= 0) {
                // my XML percentage value was 12%
                params.guidePercent = 0.12f - percentage; // up
                //params.guidePercent = 0.12f + percentage; // downm
                guideTopInfo.setLayoutParams(params);

                //constraintSet.setGuidelinePercent(R.id.guideline2, 0.12f - percentage);
                //TransitionManager.beginDelayedTransition(constraintLayout);
                //constraintSet.applyTo(constraintLayout);
            }
        }
    });
}

My other answer about parallax if anyone is interested. ;)

Solution 2 - Android

This code will change you guidePercent to 0

ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) guideline.getLayoutParams();
lp.guidePercent = 0;
guideline.setLayoutParams(lp);

Solution 3 - Android

guideline.setGuidelinePercent(value: Float)

This is enough to change the guideline percent during runtime.

Solution 4 - Android

you cam make it center doing it worked with me i hope it helps

<androidx.constraintlayout.widget.Guideline
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintGuide_percent="0.5"
                    app:layout_constraintStart_toStartOf="parent" />

Solution 5 - Android

You need to use ConstraintSet object to set layout_constraintGuide_percent in code.

Below is the code to set layout_constraintGuide_percent in code. First you need to create constraint set object, call clone method passing constraintlayout and then call setGuidelinePercent passing guideline id and ratio.

    ConstraintSet constraintSet = new ConstraintSet();
    constraintSet.clone(constraintLayout);

    constraintSet.setGuidelinePercent(R.id.guideline, 0.4f);

Solution 6 - Android

In Kotlin & inside Fragment or other activities,set GuideLine app: related parameters has a little difficulty , but I Solved it in this way :

 val view = inflate(this, R.layout.ly_custom_menu_item_wallet_side_menu, null)
 val guideL = view.findViewById<Guideline>(R.id.guideline_wallet)
          var constParam: ConstraintLayout.LayoutParams = guideL.layoutParams as ConstraintLayout.LayoutParams
          constParam.guidePercent = 0.42f
          guideL.layoutParams = constParam

and this is Guideline element inside our ly_custom_menu_item_wallet_side_menu.xml layout :

 <androidx.constraintlayout.widget.Guideline 
    android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/guideline_wallet"
    android:orientation="vertical" 
   app:layout_constraintGuide_end="84dp"  />

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMithun Sarker ShuvroView Question on Stackoverflow
Solution 1 - AndroidNícolas SchirmerView Answer on Stackoverflow
Solution 2 - AndroidVadymView Answer on Stackoverflow
Solution 3 - AndroidShashank MishraView Answer on Stackoverflow
Solution 4 - Androideng mohamed emamView Answer on Stackoverflow
Solution 5 - AndroidArnav RaoView Answer on Stackoverflow
Solution 6 - AndroidHamed JalilianiView Answer on Stackoverflow