ConstraintLayout: change constraints programmatically
To set constraints of image view to:
app:layout_constraintRight_toRightOf="@+id/check_answer1"
app:layout_constraintTop_toTopOf="@+id/check_answer1"use:
ConstraintLayout constraintLayout = findViewById(R.id.parent_layout);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
constraintSet.connect(R.id.imageView,ConstraintSet.RIGHT,R.id.check_answer1,ConstraintSet.RIGHT,0);
constraintSet.connect(R.id.imageView,ConstraintSet.TOP,R.id.check_answer1,ConstraintSet.TOP,0);
constraintSet.applyTo(constraintLayout);To set constraints of image view to:
app:layout_constraintRight_toRightOf="@+id/check_answer2"
app:layout_constraintTop_toTopOf="@+id/check_answer2"use:
ConstraintLayout constraintLayout = findViewById(R.id.parent_layout);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
constraintSet.connect(R.id.imageView,ConstraintSet.RIGHT,R.id.check_answer2,ConstraintSet.RIGHT,0);
constraintSet.connect(R.id.imageView,ConstraintSet.TOP,R.id.check_answer2,ConstraintSet.TOP,0);
constraintSet.applyTo(constraintLayout);
How can i change constraint programmatically
First add:
android:id="@+id/constraint_layout"
to your ConstraintLayout, then change:
android:onClick="on_expand"
in:
android:onClick="onExpand"
to respect lowerCamelCase
methods naming convention.
Finally this should be your code:
public class MainActivity extends AppCompatActivity {
private ConstraintLayout mConstraintLayout;
private Button mButton;
private ConstraintSet mConstraintSet = new ConstraintSet();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mConstraintLayout = findViewById(R.id.constraint_layout);
mButton = findViewById(R.id.button2);
}
public void onExpand(View view) {
// If the button is already visible then you don't need to apply any constraints
if (mButton.getVisibility() == View.VISIBLE) {
return;
}
/* Otherwise:
- Make the button2 visible;
- Insert the actual constraints in the ConstraintSet
- Define a new constraint between button3 and button2 (TopToBottomOf)
- Apply it to the ConstraintLayout */
mButton.setVisibility(View.VISIBLE);
mConstraintSet.clone(mConstraintLayout);
mConstraintSet.connect(R.id.button3, ConstraintSet.TOP,
R.id.button2, ConstraintSet.BOTTOM);
mConstraintSet.applyTo(mConstraintLayout);
}
}
How to contrain a view programmatically to parent in ConstraintLayout
From the docs there is a PARENT_ID
in the ConstraintSet to use as the ID of constraints.
Also set setHorizontalBias
and setVerticalBias
methods as well.
The accepted answer of you referenced slashdot question shows you how to use a ConstraintSet
Removing/Adding constraint programmatically in ConstraintLayout
I have not worked through your code, but the following illustrates how to break and make the constraint using ConstraintSet
.
ConstraintSet set = new ConstraintSet();
ConstraintLayout layout;
layout = (ConstraintLayout) findViewById(R.id.layout);
set.clone(layout);
// The following breaks the connection.
set.clear(R.id.bottomText, ConstraintSet.TOP);
// Comment out line above and uncomment line below to make the connection.
// set.connect(R.id.bottomText, ConstraintSet.TOP, R.id.imageView, ConstraintSet.BOTTOM, 0);
set.applyTo(layout);
move Constraint Layout programmatically
Instead of layoutLocation.visibility = View.INVISIBLE
try layoutLocation.visibility = View.GONE
How to programmatically add views and constraints to a ConstraintLayout?
I think you should clone the layout after adding your ImageView.
ConstraintLayout layout = (ConstraintLayout)findViewById(R.id.mainConstraint);
ConstraintSet set = new ConstraintSet();
ImageView view = new ImageView(this);
view.setId(View.generateViewId()); // cannot set id after add
layout.addView(view,0);
set.clone(layout);
set.connect(view.getId(), ConstraintSet.TOP, layout.getId(), ConstraintSet.TOP, 60);
set.applyTo(layout);```
Related Topics
List Filter Custom Adapter Dont Give Result
How to Use Algolia Query in Firestorerecycleroptions
How to Have Placeholders in Strings.Xml for Runtime Values
How to Underline Text in an Android Layout
How to Add a Fragment to an Activity with a Programmatically Created Content View
How to Get the Resource Id of an Image If I Know Its Name
How to Solve Java.Lang.Outofmemoryerror Trouble in Android
Trying to Attach a File from Sd Card to Email
Where Does Android Emulator Store SQLite Database
How Set Maximum Date in Datepicker Dialog in Android
How to Connect to More Than One Firebase Database from an Android App
How to Hang Up Outgoing Call in Android
How to Display Image from Url on Android
Android Youtube App Play Video Intent
Disabling the Fullscreen Editing View for Soft Keyboard Input in Landscape
Android Starting Service At Boot Time , How to Restart Service Class After Device Reboot