Alperen Enes Bayar
Alperen Enes Bayar

Reputation: 89

ScrollView doesn't stretch enough

I have a LinearLayout(horizontal) inside a ScrollView. I'm trying to add TextView's and Button's inside LinearLayout with programmatically.

My code works without errors but with little glitch. My ScrollView only stretch for TextView not for Button's. Sorry for the inadequate explanation, maybe screenshot helps: Imgur.

❒ Eve gir. is a Button the rest is TextView.

How can I make ScrollView stretch and wrap my Button's too. i tried set the fillViewPort but it did not work.

TextView adding code:

 private fun createText(prTx: String, color: String) {

    val text = TextView(this)
    text.text = prTx
    text.gravity = Gravity.CENTER_HORIZONTAL
    text.textSize = 18F
    text.setShadowLayer(5F,3F,2F,Color.BLACK)

    when (color) {
        "n" -> text.setTextColor(Color.WHITE)
        "p" -> text.setTextColor(Color.rgb(255,182,193))
        "m" -> text.setTextColor(Color.rgb(182,207,255))
        "cm" -> text.setTextColor(Color.rgb(182, 242, 227))
        "olay" -> {
            text.setShadowLayer(5F,3F,2F,Color.rgb(100,0,166))
            text.setTextColor(Color.rgb(75,0,130))
        }
    }

    text.textAlignment = TextView.TEXT_ALIGNMENT_CENTER
    text.layoutParams = LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT)
    val layout = findViewById<LinearLayout>(R.id.llText)
    layout.addView(text)
}

Button adding code

private fun createButton(prTx: String, clk: Int) { 
    val button = Button(this)
    button.text = prTx
    button.setBackgroundColor(Color.TRANSPARENT)
    button.gravity = Gravity.CENTER_HORIZONTAL;
    button.textSize = 18F
    button.transformationMethod = null;
    button.setTextColor(Color.WHITE)
    button.setShadowLayer(10F,5F,5F, Color.BLACK)
    button.textAlignment = TextView.TEXT_ALIGNMENT_CENTER

    button.setOnClickListener {
        when (clk) {         
        }
    }

    button.layoutParams = LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT)
    val layout = findViewById<LinearLayout>(R.id.llText)
    layout.addView(button)
}

Layout's XML codes

    <ScrollView
    android:id="@+id/sv"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="16dp"
    android:layout_marginBottom="16dp"
    android:background="#5B34515E"
    android:fadeScrollbars="false"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="@+id/ivMutfak"
    app:layout_constraintHeight_default="wrap"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="@+id/clMiddle"
    app:layout_constraintTop_toBottomOf="@+id/clMiddle"
    app:layout_constraintVertical_bias="0.0">

    <LinearLayout
        android:id="@+id/llText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:orientation="vertical" />
</ScrollView>

Upvotes: 1

Views: 160

Answers (2)

Alperen Enes Bayar
Alperen Enes Bayar

Reputation: 89

The problem was not ScrollView, it was LinearLayout. Problem solved when I deleted it android:layout_margin's of `LinearLayout. I still don't know why but this solution worked for me.

Upvotes: 2

C&#244;ng Hải
C&#244;ng Hải

Reputation: 5241

You should use LinearLayout.LayoutParams.WRAP_CONTENT instead of ViewGroup.LayoutParams.MATCH_PARENT. Try this code

LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT)

Upvotes: 0

Related Questions