Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Guideline percentage in constraint layout programmatically

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.

like image 762
Mithun Sarker Shuvro Avatar asked Dec 11 '16 10:12

Mithun Sarker Shuvro


People also ask

How is guideline used in constraint layout?

There are two types of guidelines: Now we can position guidelines in three different ways: By using (layout_constraintGuide_begin) to specify a fixed distance from the left or the top of a layout. By using (layout_constraintGuide_end) to specify a fixed distance from the right or the bottom of a layout.

How do I change constraint layout?

Open the layout file (activity_main. xml) in Android Studio and click the Design tab at the bottom of the editor window. In the Component Tree window, right-click LinearLayout and then choose Convert layout to ConstraintLayout from the context menu.

What is the barrier and guideline in the constraint layout?

A Barrier references multiple widgets as input, and creates a virtual guideline based on the most extreme widget on the specified side. For example, a left barrier will align to the left of all the referenced views.


2 Answers

There is two ways to do it:

  • Using ConstraintLayout.LayoutParams

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); 
  • Using ConstraintSet

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. ;)

like image 59
Nícolas Schirmer Avatar answered Sep 17 '22 15:09

Nícolas Schirmer


This code will change you guidePercent to 0

ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) guideline.getLayoutParams(); lp.guidePercent = 0; guideline.setLayoutParams(lp); 
like image 38
Vadym Avatar answered Sep 17 '22 15:09

Vadym