Thursday, 25 November 2021

Dynamic TextView not displaying properly

I am generating some TextViews dynamically in a loop. They are being displayed but I am not able create gap among them. They shows up as one row instead of separate rows.

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView 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:fitsSystemWindows="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    >

        <LinearLayout

            android:id="@+id/ll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="18dp"
            android:paddingRight="18dp"
            android:paddingTop="60dp"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:focusableInTouchMode="true"
            android:orientation="vertical">

        </LinearLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

tviewlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/light"
    android:textAlignment="gravity"
    android:padding="10dp"
    android:layout_margin="10dp"
    android:textSize="20sp" />

Java Code

LinearLayout linearLayout =  (LinearLayout) binding.ll;
                for (int i=0; i<arrSplit.length; i++)
                {
                    TextView tv = (TextView)getLayoutInflater().inflate(R.layout.tviewlayout, null);
                    tv.setText(arrSplit[i]);
                    tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                    linearLayout.addView(tv);
                }


from Dynamic TextView not displaying properly

No comments:

Post a Comment