Search code examples
androidandroid-linearlayoutandroid-viewandroid-inflate

Programmatically Adding views in inflated layout


I am trying to add views in linear layout programmatically which is a part of inflated view but after adding views in linear layout it is not displaying

MainActivity code:-

       final LayoutInflater inflater = this.getLayoutInflater();
            View view = inflater.inflate(R.layout.suscription_alert,null);
            LinearLayout ll = view.findViewById(R.id.LinearLayout);

       for(int i =0;i<4;i++) {
                TextView t = new TextView(MainActivity.this);
                t.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                t.setText("Hello");
                ll.addView(t);
            }
            view.invalidate();

        AlertDialog alert = new AlertDialog.Builder(MainActivity.this)
                .setView(view)
                .create();
        Objects.requireNonNull(alert.getWindow()).setBackgroundDrawableResource(android.R.color.transparent);

        alert.show();

Subscription Alert layout:-

    <?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:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="SUBSCRIPTIONS"
        android:textColor="#F5D90A"
        android:textSize="23sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <LinearLayout
        android:id="@+id/LinearLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:paddingTop="10dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />

</androidx.constraintlayout.widget.ConstraintLayout>

output:

enter image description here

programmatically added views are not showing here, How to solve this problem and why it is happening anybody have any idea?


Solution

  • Your LinearLayout needs to have height greater than 0 to add views at run-time otherwise it won't be able to calculate the height.

    For example make it wrap_content

    <?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:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="SUBSCRIPTIONS"
            android:textColor="#F5D90A"
            android:textSize="23sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    
        <LinearLayout
            android:id="@+id/LinearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="10dp"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView3" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>