2

It's supposed that WRAP_CONTENT does this:

Special value for the height or width requested by a View. WRAP_CONTENT means that the view wants to be just large enough to fit its own internal content, taking its own padding into account.

But the reality is different

If you create a very long textview with a lot of height, bigger than the screen height, then, WRAP_CONTENT applied to the height of that textview has an erroneal behaviour, because limits the textview height to the screen height.

android:layout_height="wrap_content"

Why WRAP_CONTENT has that erroneal behaviour?

How can you set to a view to have the size of it's content without being limited by the screen height?

Complete XML file (this is a screen for end titles of a game, the text will be animated up, because of that is hidden below the screen):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/creditsTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="@string/credits_big"
        android:textAlignment="center"
        android:textColor="@color/font"
        android:textSize="@dimen/medium_text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="parent" />
</android.support.constraint.ConstraintLayout>
halfer
  • 19,824
  • 17
  • 99
  • 186
NullPointerException
  • 36,107
  • 79
  • 222
  • 382
  • How have you determined the `TextView` height is limited to the screen height - also, could you explain why this behaviour would be a problem? (Just because off the top of my head I can't imagine a situation where you'd care about how text is rendered when it is off screen). – PPartisan Oct 23 '19 at 12:03
  • 2
    @PPartisan this is a screen for end titles of a game, the text will be animated up. The text is much longer than the screen height, and I determine that is cutted at screen height estimating it, don't sure 100% but 99% yes. – NullPointerException Oct 23 '19 at 12:30
  • 1
    May help: [Scrolling credits text in Android](https://stackoverflow.com/questions/23029085/scrolling-credits-text-in-android) – Ricardo A. Oct 23 '19 at 12:37
  • Also related: [Wrap_content view inside a ConstraintLayout stretches outside the screen](https://stackoverflow.com/questions/40850966/wrap-content-view-inside-a-constraintlayout-stretches-outside-the-screen) – Ricardo A. Oct 23 '19 at 12:48
  • can you explain why did you put this - ' app:layout_constraintTop_toBottomOf="parent" '. This line maybe the reason for unusual behaviour. instead try using ' app:layout_constraintBottom_toBottomOf="parent" '. – Pavan Varma Oct 23 '19 at 13:02
  • @PavanVarma it's explained in the question, this is a screen for end titles of a game, the text will be animated up, because of that is hidden below the screen – NullPointerException Oct 23 '19 at 13:21
  • It seems like the `TextView` height is limited to parents height, and `ConstraintLayout` is not a scrollable view - hence it is limited to its container (`FrameLayout`) height, which is limited to `Activity` height. If you want to simulate credit roll you should use some kind of Scrolling behavior (and maybe change `translationY` value). Check `TextView.getDesiredHeight()`. – Vitalii Malyi Oct 23 '19 at 13:21

1 Answers1

0

Try the following to get the true height of the text in the TextView and to resize the view to match.

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mainLayout.doOnLayout {
            val tv = findViewById<TextView>(R.id.creditsTextView)
            val layout = tv.layout
            val lp = tv.layoutParams
            lp.height = layout.height
            tv.layoutParams = lp
        }
    }
}

Although this works, it makes me feel a little uneasy. You may want to look at other ways to accomplish your credits scroll.

Cheticamp
  • 61,413
  • 10
  • 78
  • 131