What is the equivalent of layout_constraintRight_toLeftOf programmatically in android?
You can set like this,
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(layout); // root layout
// If you want to align parent to the layout left
constraintSet.connect(textView.getId(), ConstraintSet.LEFT, layout.getId(), ConstraintSet.LEFT);
// If you want to align to the left of one more textview - second text view
constraintSet.connect(textView.getId(), ConstraintSet.RIGHT, textView1.getId(), ConstraintSet.LEFT);
/*______________________________
| Text1 Text2 |
| |*/
constraintSet.applyTo(layout);
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);
}
}
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);
Changing width and height in ConstraintLayout programmatically doesn't work
You're doing it wrong. You've to apply a new ConstraintSet
like:
val constraintSet1 = ConstraintSet()
constraintSet1.clone(yourConstraintLayout)
val constraintSet2 = ConstraintSet()
constraintSet2.clone(yourConstraintLayout)
constraintSet2.constrainWidth(R.id.one, 0)
constraintSet2.constrainWidth(R.id.two, 0)
constraintSet2.constrainHeight(R.id.one, 0)
constraintSet2.constrainHeight(R.id.two, 0)
// to switch to full screen
constraintSet2.applyTo(yourConstraintLayout)
// to get back to your original screen
constraintSet1.applyTo(yourConstraintLayout)
Android ConstraintLayout: set one element in the center of another one
If you have to set it in the center just change the constraints of the image with :
app:layout_constraintBottom_toBottomOf="@id/login_next_btn"
app:layout_constraintTop_toTopOf="@id/login_next_btn"
app:layout_constraintStart_toStartOf="@id/login_next_btn"
app:layout_constraintEnd_toEndOf="@id/login_next_btn"
Related Topics
Fragment Standard Transition Not Animating
Writing Some Characters Like '<' in an Xml File
Impossible to Rotate The Emulator with Android 4.4
Get Application Installed Date on Android
Notification Passes Old Intent Extras
How to Implement Ssl Certificate Pinning While Using React Native
No Resource Found That Matches The Given Name '@Style/ Theme.Holo.Light.Darkactionbar'
Add Viewpagerindicator to Android Studio
Inform Activity from a Broadcastreceiver Only If It Is in The Foreground
Audio Format for iOS and Android
How to Run The Same Asynctask More Than Once
Disable Webview Touch Events in Android
Android Edittext Listener for Cursor Position Change
How to Convert Time to " Time Ago " in Android
How to Remove Black Background Between Start New Activity During Slide_Left Animation