Home » Android » android – EditText won't write into single line

android – EditText won't write into single line

Posted by: admin June 15, 2020 Leave a comment

Questions:

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" />
How to&Answers:

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.

Answer:

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

android:inputType="text" 
android:maxLines="1"

Answer:

**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>

Answer:

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