0

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


}
sacacorchos
  • 163
  • 9

1 Answers1

0

Try to modify the existing layout params instead of creating new ones. By creating new params, you are likely losing the other information you have defined in your XML, such as your constraints. I'm going from memory, but I would try something like this:

ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) mytext.getLayoutParams();
params.setMargins(0,10,0,0);
mytext.setLayoutParams(params);

You may also be able to clone the existing layoutparams instead of modifying the existing ones:

final ConstraintLayout.LayoutParams current = 
  (ConstraintLayout.LayoutParams) mytext.getLayoutParams();
final ConstraintLayout.LayoutParams params = 
  new ConstraintLayout.LayoutParams(current);
params.setMargins(0,10,0,0);
mytext.setLayoutParams(params);

Edit: Also, consder that setMargins accepts dimensions as pixels as an argument, not density-independent pixels. This means that if you just set an int value like 10, then your layout will not scale well for different android devices. You can convert the value to DP yourself, or define the value in xml and use Resources::getDimensionPixelOffest.

PPartisan
  • 8,173
  • 4
  • 29
  • 48
  • apparently it's working, but why if I comment "mytext.setLayoutParams(params);" , it is also setting the layout params? I mean, is "mytext.setLayoutParams(params);" needed? It is setting layout params just with: "ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) mytext.getLayoutParams(); params.setMargins(0,10,0,0);" – sacacorchos Dec 22 '22 at 18:50
  • I suppose modifying the existing layout params is enough for the view to invalidate/redraw. You may want to keep using `setLayoutParams` anyway, just to ensure consistent behaviour. It may also be worth copying the existing layout params instead of modifying the existing ones, by doing `final ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams((ConstraintLayout.LayoutParams) mytext.getLayoutParams());` – PPartisan Dec 22 '22 at 18:51
  • Okay, could you please modify the answer with the better solution you have told me: copying the existing layout params instead of modifying the existing ones. – sacacorchos Dec 22 '22 at 19:25
  • I wouldn't necessarily say it is better, it's just an alternative – PPartisan Dec 22 '22 at 19:26
  • Okay, so could you post the 2 possibilities in the answer, please? – sacacorchos Dec 22 '22 at 19:37
  • Sure, I've included it in my answer – PPartisan Dec 22 '22 at 19:45
  • Has this solved your issue @sacacorchos? Could you accept/upvote the answer if so, please? – PPartisan Dec 23 '22 at 17:34
  • Yes I have been making some tests with both options and I would like to hear one final opinion. Both options are working correctly, but which one would you use better than the other, first option (modifying layout params) or second option (copying layout params)? – sacacorchos Dec 23 '22 at 19:01
  • I don't think it's important - if I were to see either approach on a code review then I would be satisfied. Personally, I would copy the layout params because I favour immutable code over mutable, but either is fine. – PPartisan Dec 23 '22 at 19:52
  • thanks, I'm accepting your answer :) – sacacorchos Dec 24 '22 at 15:47