44

I've set a TextView as the display. I want to display the operation being performed in one line, for example:

9856243187 + 2457896123 - 214583

The problem is, when the input reaches the edge of the TextView - it jumps to the next line.

So, is there anyway to avoid this behavior and keep the input in one line, even if it goes out of the screen?

Hadas Kaminsky
  • 1,285
  • 1
  • 16
  • 39
Toni Joe
  • 7,715
  • 12
  • 50
  • 69

9 Answers9

97

You should use android:maxLines="1" in your TextView tag. android:singleLine="true" is deprecated.

Copacel
  • 1,328
  • 15
  • 25
  • can't find a notice to depreciation at https://developer.android.com/reference/android/widget/TextView#attr_android:singleLine – Cor Apr 10 '22 at 09:21
21
android:singleLine="true"

is the answer

Update :

android:singleLine attribute has been deprecated since API Level 3, you can use

android:maxLines="1"
Ravi
  • 34,851
  • 21
  • 122
  • 183
13

tl;dr:

For new users seeing this question, use android:singleLine="true". Ignore deprecation warnings.

Explanation

According to this bug report, setting android:maxLines="1" may cause your application to crash when some specific values are set in android:ellipsize as well. Apparently it affects most Android devices running 4.1 Jellybean through 6.0.1 Marshmallow (being fixed in 7.0 Nougat). Depending on your device, your mileage may vary. However, if you as a developer are targeting a wide range of devices, it is best to use android:singleLine="true" in order to prevent crashes.

Jonathan Precise
  • 327
  • 3
  • 12
  • Is this still an issue now? – Micer Feb 08 '18 at 14:01
  • 3
    As long as you target a minimum SDK lower than 24, this will always be an issue. And according to the [Android Version Distribution chart for February 5, 2018](https://developer.android.com/about/dashboards/index.html), 70.4% of devices using Google Play have not been updated to Nougat (SDK24), so raising the minimum SDK not might be a viable option for many. – Jonathan Precise Feb 17 '18 at 03:32
5

add the code in your Textview

android:singleLine="true"
sasikumar
  • 12,540
  • 3
  • 28
  • 48
5

Thanks guys single line did the tricks but i also had to remove the ellipsis:

android:ellipsize="none"
Toni Joe
  • 7,715
  • 12
  • 50
  • 69
3

This code worked for me:

android:singleLine="true"
Taher A. Ghaleb
  • 5,120
  • 5
  • 31
  • 44
Maryam Azhdari
  • 1,161
  • 11
  • 8
2

If you want to manually scroll your single line content then you should use TextView inside HorizontalScrollView

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="16dp"
        android:paddingTop="16dp"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:text="Single line text view that scrolls automatically if the text is too long to fit in the widget" />
</HorizontalScrollView>
Kishan Vaghela
  • 7,678
  • 5
  • 42
  • 67
1

Set the TextView to singeline.

Take a look at this

EE66
  • 4,601
  • 1
  • 17
  • 20
1

Can you please try this one ?

<TextView
        android:text="Single line text view that scrolls automatically if the text is too long to fit in the widget" 
        android:singleLine="true" 
        android:ellipsize="marquee"
        android:marqueeRepeatLimit ="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true" 
        android:scrollHorizontally="true"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"/>

Hope this will help you.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151