Search code examples
androidandroid-layoutdynamically-generated

Dynamically set XML properties of new TextViews


In my app I have a button (Add) that takes the entered name, creates a TextView with that name and places the TextView in a LinearLayout. You should be able to create how many subjects (school subjects) you want, so the properties would need to be set in the Java code (correct me if I'm wrong). I have managed make it create the TextViews, but they have default settings. How can I make them like the first TextView? I haven't found any solution on this one nor on the Android Developers website.

Here's the XML for the LinearLayout and the first TextView:

<LinearLayout
    android:id="@+id/subjectLayout"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toTopOf="@+id/entryEditText"
    app:layout_constraintEnd_toStartOf="@+id/divider"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0">

    <TextView
        android:id="@+id/subjectMATHTextView"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Math"
        android:textAlignment="center"
        android:textSize="30sp"
        app:layout_constraintBottom_toTopOf="@+id/entryEditText"
        app:layout_constraintEnd_toStartOf="@+id/divider"
        app:layout_constraintHorizontal_bias="0.49"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.259" />

</LinearLayout>

And the Java Code:

final ProgressBar subjectMATHProgressBar = (ProgressBar) findViewById(R.id.subjectMATHProgressBar);
    final EditText entryEditText = (EditText) findViewById(R.id.entryEditText);
    Button progressBtn = (Button) findViewById(R.id.progressBtn);
    final Button addSubjectButton = (Button) findViewById(R.id.addSubjectBtn);


    // For Progress Button
    progressBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                if (!entryEditText.getText().toString().matches("")) {
                    int currentProgress = subjectMATHProgressBar.getProgress();
                    int toSet = currentProgress + Integer.parseInt(entryEditText.getText().toString());
                    subjectMATHProgressBar.setProgress(toSet);
                }
            } catch (Exception e){
                Toast.makeText(getApplication().getBaseContext(), "Enter a number", Toast.LENGTH_SHORT).show();
            }
        }
    });


    final LinearLayout subjectLayout = (LinearLayout) findViewById(R.id.subjectLayout);
    LinearLayout progressLayout= (LinearLayout) findViewById(R.id.progressLayout);

    // For Add Subject Button
    addSubjectButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {

                // Important code:
                TextView newTextView = new TextView(getApplication().getBaseContext());
                newTextView.setText(entryEditText.getText().toString());
                subjectLayout.addView(newTextView);

            } catch (Exception e){
                Toast.makeText(getApplication().getBaseContext(), "Enter a valid name", Toast.LENGTH_SHORT).show();
            }
        }
    });

Thanks in advance!


Solution

  • That is not how it works, if i get it right u need to make a list of subjects? For this purpose there is a special view - RecyclerView or ListView. It allows you to populate your layout with a list of data. Each data entry is responsible for different item in list.

    Here you can find how it works and to use it: https://developer.android.com/guide/topics/ui/layout/recyclerview