1

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.
Felix ZY
  • 674
  • 6
  • 14

1 Answers1

1

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

javdromero
  • 1,850
  • 2
  • 11
  • 19
  • I thought `singleLine` was deprecated but perhaps that only applies to `EditText`s (https://stackoverflow.com/questions/30879471/android-singleline-vs-maxlines). Will try this tomorrow. I'm a bit hesitant to using `inputType` as the docs clearly state that "Setting this attribute to anything besides none also implies that the text is editable". – Felix ZY Aug 10 '21 at 23:07
  • Since is a TextView and not an EditText it doesn't apply – javdromero Aug 11 '21 at 00:38
  • Tested and it works. AS does mark `singleLine` as deprecated in the suggestion box but not once it is written out. – Felix ZY Aug 11 '21 at 08:33