Search code examples
javaandroid-studioandroid-constraintlayout

Setting Constraints of a button dynamically in Android Studio


I have a NumberPicker in my application and according to the number that is chosen some editText appear (These editTexts are put in the xml file as invisible, and then in the java file I set them visible according to the number that is chosen). The problem is that I would like to add a button on the bottom of these editTexts, so at the start the button should be under the first editText, but if the user chooses number 2, the button shuold appear only under the second editText and so on... All the editText are put inside a linear layout. For a better explanation, I put two images related to what I would like to get.

enter image description here enter image description here

My xml file is the following one:

<LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center_vertical"
            android:padding="30dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/numberPicker">
<EditText
                android:id="@+id/text1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="0dp"
                android:paddingRight="43dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="parent"
                android:hint="Inserisci la location"
                android:inputType="text"
                android:textSize="18sp"
                android:textStyle="bold"
                android:visibility="visible"
                />
 <TextView
                android:id="@+id/getCurrentLocation"
                android:layout_width="wrap_content"
                android:layout_height="35dp"
                android:background="#00FFFFFF"
                android:backgroundTint="#01FFFFFF"
                android:paddingLeft="90dp"
                android:backgroundTintMode="multiply"
                android:text="Get my current location"
                android:textStyle="bold" />


  <Button
                android:id="@+id/button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/button_rounded"
                android:text="Search Places"
                android:textColor="#ffffff"
                android:textSize="22sp" />
  <EditText
                android:id="@+id/text2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="0dp"
                android:paddingRight="43dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="parent"
                android:hint="Inserisci la location"
                android:inputType="text"
                android:textSize="18sp"
                android:textStyle="bold"
                android:visibility="invisible"
                />
   etc... 
   <EditText
                android:id="@+id/text20"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="0dp"
                android:paddingRight="43dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="parent"
                android:hint="Inserisci la location"
                android:inputType="text"
                android:textSize="18sp"
                android:textStyle="bold"
                android:visibility="invisible"
                />

The Java file is the following one:

mtext1 = (EditText) findViewById(R.id.text1);
mtext2 = (EditText) findViewById(R.id.text2);
etc...
mtext20 = (EditText) findViewById(R.id.text20);
mGetPlaces = (Button) findViewById(R.id.button);


numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {


        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            
            switch (newVal) {
                case 1:
                    mtext2.setVisibility(View.INVISIBLE);
                    mtext3.setVisibility(View.INVISIBLE);
                    etc...
                    mtext20.setVisibility(View.INVISIBLE);
                    
                 break;

                case 2:
                    mtext2.setVisibility(View.VISIBLE); 
                    mtext3.setVisibility(View.INVISIBLE); 
                    etc...
                    mtext20.setVisibility(View.INVISIBLE); 
                break;

                etc...

                case 20:
                    mtext2.setVisibility(View.VISIBLE); 
                    mtext3.setVisibility(View.VISIBLE); 
                    etc...
                    mtext20.setVisibility(View.VISIBLE); 

                    break;
            }
        }    

I have tried to put the following code inside case 2, but the button totally disappear and no error appears.

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mGetPlaces.getLayoutParams();
params.setMarginStart(mtext2.getId());
mGetPlaces.setLayoutParams(params);

In xml there is the possibility to set

app:layout_constraintTop_toBottomOf="@+id/name"

However, I didn't find any solution to apply that to the java code.


Solution

  • Take a look at ConstraintSet. There are some tutorials online on how to use ConstraintSets to create and modify constraints programmatically.

    You might also make use of GONE widgets. The second EditText could have a visibility of "gone" so that the bottom button would float up to be under the first EditText and float down when the second EditText is visible.