Search code examples
xmlandroid-studiokotlinscrollview

ScrollView populated dynamically with TextViews is exceeding "bounds" of view


I am trying to build a layout with the following structure:

Header Image

Scrollview (populated dynamically with Textviews)

Static Menu Bar

example of layout I want

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/root"
    tools:context=".ProfileActivity"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/profile_pic"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.071"
        tools:srcCompat="@tools:sample/avatars" />



        <ScrollView
            android:id="@+id/scrolly"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/ll"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                ></LinearLayout>

        </ScrollView>


    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="128dp"
        android:layout_alignBottom="@+id/profile_pic"
        android:layout_gravity="bottom"
        android:layout_marginStart="8dp"
        android:layout_marginBottom="-608dp"
        app:itemBackground="@color/colorPrimary"
        app:itemIconTint="@drawable/selector"
        app:itemTextColor="@drawable/selector"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/profile_pic"
        app:layout_constraintVertical_bias="1.0"
        app:menu="@menu/menu_nav" />


</RelativeLayout>

I am dynamically creating textviews and inserting them into a linearlayout within a scrollview but it ends up covering up the imageview once it is populated and my static menubar is nowhere to be found.

I am not sure what would cause this type of behavior What is the best practice in this case for populating my ScrollView?

Here is the code:

//create and display textviews of current inventory
private fun displayInventory(){

    var ll : LinearLayout = findViewById(R.id.ll)

    userInventory.forEach { element ->
        var uId = element.Uid.toString()
        var name = element.name.toString()
        var purchDate = element.purchDate.toString()
        var country = element.country.toString()
        var local = element.local.toString()
        var mine = element.mine.toString()
        var weight = element.weight.toString()
        var paid = element.paid.toString()
        var asking = element.asking.toString()
        var description = element.description.toString()
        var dimensions = element.dimensions.toString()
        var filePaths: ArrayList<String> = element.filePaths
        var downloadUrls: ArrayList<String> = element.downloadUrls

        val txtView = TextView(this)
        txtView.text = "Uid: $name\n" +
                "purchase date: $purchDate\n" +
                "country: $country\n" +
                "local: $local\n" +
                "mine: $mine\n" +
                "weight: $weight\n" +
                "paid: $paid\n" +
                "asking: $asking\n" +
                "UId: $uId\n" +
                "dimension: $dimensions\n" +
                "FilePaths:     ${filePaths.toString()}\n" +
                "downloadUrls: ${downloadUrls.toString()}\n"
        txtView.setTextColor(Color.BLACK)
        val params = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT,
        )
        txtView.layoutParams = params
        txtView.textSize = 25f
        ll.addView(txtView)

    }
}

The result of my code What view ends up looking like

Any help is much appreciated or if you see I am not following any other best practices


Solution

  • I don't know why you use RelativeLaoyut if ConstraintLayout works better than it. So, I changed it to ConstraintLayout. 1st- your bottom navigation height which is usually 56dp and I added it to 56dp if you want your bottom Navigation size app default then use ?windowActionBar in your height of NavigationBar. 2nd - Use the Nested Scroll view for a more efficient result. your new code is like the below.

    <?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:id="@+id/root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    
        <ImageView
            android:id="@+id/profile_pic"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:srcCompat="@tools:sample/avatars" />
    
        <androidx.core.widget.NestedScrollView
            android:id="@+id/scrolly"
            android:layout_width="match_parent"
            android:layout_height="0dp"
    
            android:fillViewport="true"
            app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/profile_pic">
    
            <LinearLayout
                android:id="@+id/ll"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" />
    
        </androidx.core.widget.NestedScrollView>
    
    
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_nav"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            app:itemBackground="@color/colorPrimary"
            app:itemIconTint="@drawable/selector"
            app:itemTextColor="@drawable/selector"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:menu="@menu/menu_nav" />
    
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    If you facing the same problem. let me know