0

I have a working layout in .xml file no problem here. In my code there is a button that show or hide these two imageViews. When I hide the two imageViews I want to set the checkbox layout constraints to mach parent instead of matching constraints("0dp"), and when I show the the two imageViews I want to set checkbox layout to match constraints.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/constraint_child"
android:layout_width="match_parent"
android:layout_height="wrap_content">


<ImageView
    android:id="@+id/delete_child"
    android:layout_width="36dp"
    android:layout_height="36dp"
    android:layout_marginStart="16dp"
    android:src="@drawable/delete"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/edit_child"
    app:layout_constraintTop_toTopOf="parent" />

<CheckBox
    android:id="@+id/checkbox_child"
    android:layout_width="0dp"
    android:layout_height="42dp"
    android:text="CheckBox"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/edit_child"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageView
    android:id="@+id/edit_child"
    android:layout_width="26dp"
    android:layout_height="26dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/delete_child"
    app:layout_constraintStart_toEndOf="@+id/checkbox_child"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/edit" />


</androidx.constraintlayout.widget.ConstraintLayout>

I don't want to create the CheckBox and the two ImageViews programmatically ,because I have created them before in .xml file. what I want is changing these views programmatically in java. Thanks in advance.

Omer123
  • 145
  • 2
  • 10
  • If using kotlin, you can use `View.updateLayoutParams {} ` extension function to update layout. – Darshan Jul 07 '22 at 20:37
  • @DarShan my project is in java and I am new to Kotlin. Any idea how to do it in Java? – Omer123 Jul 07 '22 at 20:52
  • You can manually get the existing layout params, make changes & set the new params to the required view. Something like this: https://stackoverflow.com/a/10160317 – Darshan Jul 07 '22 at 20:53

2 Answers2

1

programaticlly on button onclicklistner When images are gone you can set

Checkbox checkbox = findViewById(R.id.checkbox_child);
    
if (myImageView.getVisibility() == View.VISIBLE) {
    // Its visible
 

LayoutParams lp = new LayoutParams(0, 42);
checkbox.setLayoutParams(lp);

} else {
    // Either gone or invisible

LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, 42);
checkbox.setLayoutParams(lp);
}
ARCHER
  • 92
  • 3
1

If i understood what you are trying to do, then there is a simple way of doing it programatically : in onCreate() you declare your views

ImageView deleteIv = findViewById(R.id.delete_child);
ImageView editIv = findViewById(R.id.edit_child);
CheckBox checkbox = findViewById(R.id.checkbox_child);

Then you set an onClickListener over your button and you have to put this code inside of onClick() method generated by the button's listener

yourButton.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                deleteIv.setVisibility(View.GONE);
                editIv.setVisibility(View.GONE);
                
                // Creating a new LayoutParams object to set to the checkBox
                
                LinearLayout.LayoutParams lparams = new TableLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATH_PARENT);
                // Setting the new layout parameters to the checkbox as wanted
                checkbox.setLayoutParams(lparams);
            }
        });