Search code examples
androidandroid-layouttextviewandroid-animation

Why are words that go outside a single line TextView's bounds not rendered and clipped?


I have a TextView enclosed in a container like this:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="40dp"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:maxLines="1"
        android:text="Hi World" />

</FrameLayout>

A width on the FrameLayout of 40 dp is not quite enough to enclose the full line of text. I would therefore expect the TextView to be clipped by its parent. Instead, the TextView does not render the word "World" at all!

Expected Actual
Expected - Text is clipped Actual  - Second word not rendered
  1. Why is the second word not rendered?
  2. How can I get the TextView to be clipped instead? This is part of a width changing animation (container resized to 0dp width) where removing letters or chunks of text causes annoying flickering.

Solution

  • This works as you expected

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
    
            android:singleLine="true"
            android:ellipsize="none"
    
            android:maxLines="1"
            android:text="Hi World" />
    

    Also this

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
    
            android:inputType="text"
    
            android:maxLines="1"
            android:text="Hi World" />
    

    enter image description here