I have the following ContstraintLayout:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/my_text"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="@string/txt_string"
app:layout_constraintBottom_toBottomOf="@id/one"
app:layout_constraintEnd_toStartOf="@id/two"
app:layout_constraintStart_toEndOf="@id/three"
app:layout_constraintTop_toTopOf="@id/four"
android:layout_marginTop="@dimen/margin_dim"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
I want to change the marginTop value in code:
This is what I have done but the AppCompatTextView is disappearing:
AppCompatTextView mytext;
private void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
mytext = findViewById(R.id.my_text);
ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(0,10,0,0);
mytext.setLayoutParams(params);
}