9

I'm making a calculator and user input is contained in EditText. Input is typed through buttons on screen, so Android keyboard is disabled. However, I want it to write all input in one line and when line reaches border, it needs to be ellipsized and then horizontally scrollable so you can reach whole text.

I tried doing this by setting android:maxLines="1" and android:ellipsize="end", but that doesn't work. Text is still in multiple lines, but only one line is visible and you need to scroll vertically to see other lines. I also tried using android:singleLine="true", but that makes EditText completely unusable.

This is EditText XML:

<EditText
     android:id="@+id/textView"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_gravity="end"
     android:background="@color/colorBlue"
     android:textAlignment="textEnd"
     android:textSize="40sp" />
leonz
  • 1,107
  • 2
  • 10
  • 32

4 Answers4

14

I think this will work for you. Let me know if it works.

<EditText
 android:id="@+id/textView"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_gravity="end"
 android:background="@color/colorBlue"
 android:textAlignment="textEnd"
 android:textSize="40sp"
 android:inputType="text" 
 android:maxLines="1"
 android:lines="1"
 android:scrollHorizontally="true"
 android:ellipsize="end"/>

This post might help you as well.

Community
  • 1
  • 1
Charles Li
  • 1,835
  • 3
  • 24
  • 40
  • 7
    This + android:inputType="text" worked for me. Seems like inputType is crucial here. The link you provided helped me a lot. Ty – leonz Jun 25 '16 at 14:31
  • Np, thanks for letting me know as well. I'll update the answer with it then. Glad it helped! – Charles Li Jun 25 '16 at 14:32
4

For single line in EditText, you just need to set two properties:

android:inputType="text" 
android:maxLines="1"
SANAT
  • 8,489
  • 55
  • 66
1

Adding this line in the edittext make scrollable singleline text. android:inputType="textShortMessage"

pz64_
  • 2,212
  • 2
  • 20
  • 43
0
**This example helps you to create multiline EditText in Android.**  

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/rl"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        tools:context=".MainActivity"
        >

        <!--

            android:scrollbars="vertical"
                It will display a vertical scrollbar if the EditText text exceed
                3 lines (android:maxLines).
        -->
        <EditText
            android:id="@+id/et"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_marginBottom="10dp"
            android:hint="Multiline EditText by XML layout"
            android:fontFamily="sans-serif-condensed"
            android:background="#d3d7b6"
            android:minLines="2"
            android:maxLines="3"
            android:scrollbars="vertical"
            android:inputType="textMultiLine"
            />
        <EditText
            android:id="@+id/et2"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:fontFamily="sans-serif-condensed"
            android:hint="Multiline EditText programmatically"
            android:background="#d4d7a5"
            android:layout_below="@+id/et"
            />
    </RelativeLayout>
Vishal Sharma
  • 1,051
  • 2
  • 8
  • 15