android - How to programmatically access the current constraints of views in a ConstraintLayout? -
i can programmatically modify constraints of view in constraintlayout using constraintset before applying modifications, want test hasn't been done already. need programmatically access current constraints of view in constraintlayout.
for example, here want remove constraint between top of textview 'title' , bottom of textview view 'subtitle'. constrain top of textview 'title' bottom of 'videoview'. first need check if textview 'title' isn't constrained bottom of 'videoview' (code modified android's official website). note, can't insert final line of html on website, it's /android.support.constraint.constraintlayout in angle brackets.
public class mainactivity extends appcompatactivity { constraintset mconstraintset = new constraintset(); // create constraint set constraintlayout mconstraintlayout; // cache constraintlayout @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mconstraintlayout = (constraintlayout) findviewbyid(r.id.root); mconstraintset.clone(mconstraintlayout); // constraints constraintset } public void userdoessomething(view v) { //test if title has been constrained bottom of videoview if (mconstraintlayout 'title' not constrained bottom of 'videoview'){ mconstraintset.connect(r.id.title, constraintset.top, r.id.videoview, constraintset.bottom); // set new constraints constraintset mconstraintset.applyto(mconstraintlayout); // set new constraints constraintlayout } }
}
<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:id="@+id/root" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.test.mainactivity" android:orientation="vertical"> <videoview android:id="@+id/videoview" android:layout_width="match_parent" android:layout_height="100dp" app:layout_constrainttop_totopof="parent" app:layout_constraintleft_toleftof="parent" /> <textview android:id="@+id/subtitiles" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constrainttop_tobottomof="@+id/videoview" app:layout_constraintleft_toleftof="parent" android:gravity="center" android:text="the problem pond there many toads" android:textsize="25sp" android:background="@android:color/white" /> <textview android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constrainttop_tobottomof="@+id/subtitiles" app:layout_constraintleft_toleftof="parent" android:gravity="center" android:text="toad" android:textsize="25sp" android:background="@android:color/white" />
to acknowledge, there isn't method that, hovewer can try comparate 2 constraintlayout.layoutparams, exemple :
public class mainactivity extends appcompatactivity { constraintset mconstraintset = new constraintset(); // create constraint set constraintlayout mconstraintlayout; // cache constraintlayout int mconstraintvalue; textview mtextview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mconstraintlayout = (constraintlayout) findviewbyid(r.id.root); mtextview = (textview) findviewbyid(r.id.title); mconstraintset.clone(mconstraintlayout); // constraints constraintset constraintlayout.layoutparams params = (constraintlayout.layoutparams) mtextview.getlayoutparams(); mconstraintvalue = params.toptobottom; } public void userdoessomething(view v) { //test if title has been constrained bottom of videoview constraintlayout.layoutparams params = (constraintlayout.layoutparams) mtextview.getlayoutparams(); if (mconstraintvalue.equals(params.toptobottom)){ mconstraintset.connect(r.id.title, constraintset.top, r.id.videoview, constraintset.bottom); // set new constraints constraintset mconstraintset.applyto(mconstraintlayout); // set new constraints constraintlayout } } }
hope helps.
Comments
Post a Comment