Loading xml file programmatically into View in Android App Studio

40 views Asked by At

I am trying to programmatically add a simple xml file with some text and spinner views once the OnItemSelectedListener() is set off. The xml file would should show up in the middle of the screen, so I would like everything above to stay the same, and then everything below to be pushed down so everything is visible. I already put it in a scrollable layout so I don't need to worry about the screen size and what is showing.

I tried the following Stack Overflow links for guidance but neither seemed to cover the issue that I am facing at the moment: loading programmatically and loading view

This is what I have in the OnItemSelectedListener() so far:

        _inclusionLayout.removeAllViews();   //The relative layout that is going to hold the xml file
        View inclusionView = getLayoutInflater().inflate(R.layout.patientpage_10, _inclusionLayout, false);                              //inflating the view within the relative layout
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        inclusionView.setLayoutParams(layoutParams);
        _inclusionLayout.addView(inclusionView);
        _inclusionLayout.setVisibility(View.VISIBLE);
        Log.d("Message", "itemSelected Called"); // checking to see if the item was selected and the code executed without error

For context, this is the xml file I am trying to include:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/ptpg10_layout">

    <EditText
        android:id="@+id/ptpg10_txtMeals"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Meal"
        android:inputType="text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Spinner
        android:id="@+id/ptpg10_percentageSpinner"
        android:layout_width="271dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ptpg10_txtMealWarning" />

    <TextView
        android:id="@+id/ptpg10_txtMealWarning"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Please Enter Meal"
        android:textColor="#bf1408"
        android:visibility="gone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ptpg10_txtMeals" />

    <TextView
        android:id="@+id/ptpg10_txtSpinnerWarning"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Please Enter Percentage"
        android:textColor="#bf1408"
        android:visibility="gone"
        app:layout_constraintEnd_toEndOf="@+id/ptpg10_percentageSpinner"
        app:layout_constraintStart_toStartOf="@+id/ptpg10_percentageSpinner"
        app:layout_constraintTop_toBottomOf="@+id/ptpg10_percentageSpinner" />

    <EditText
        android:id="@+id/ptpg10_txtNotes"
        android:layout_width="340dp"
        android:layout_height="94dp"
        android:ems="10"
        android:layout_marginTop="20dp"
        android:hint="Notes (Optional)"
        android:inputType="text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ptpg10_txtSpinnerWarning" />
</androidx.constraintlayout.widget.ConstraintLayout>

However, with all of this, the screen just stays exactly the same and I cannot see any new views while seeing the message in the logcat implying that the whole thing ran smoothly with no errors. Please let me know what I am doing wrong/ any easier way to go about this.

Thank you!

0

There are 0 answers