Thursday, August 20, 2015

How to create multiline EditText in Android

activity_main.xml

<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:background="#f3f1eb"
    >
    <!--
        android:minLines
            Makes the TextView be at least this many lines tall.
            When used on an editable text, the inputType attribute's
            value must be combined with the textMultiLine flag for
            the minLines attribute to apply.

        android:maxLines
            Makes the TextView be at most this many lines tall.
            When used on an editable text, the inputType attribute's
            value must be combined with the textMultiLine flag for
            the maxLines attribute to apply.

        android:scrollbars
            Defines which scrollbars should be displayed on scrolling or not.
            Must be one or more (separated by '|') of the following constant values.

            none : No scrollbar is displayed.
            horizontal : Displays horizontal scrollbar only.
            vertical : Displays vertical scrollbar only.

        android:inputType
            The type of data being placed in a text field,
            used to help an input method decide how to let the user enter text.

            textMultiLine : Can be combined with text and its variations to allow
                            multiple lines of text in the field. If this flag
                            is not set, the text field will be constrained
                            to a single line. Corresponds to
                            TYPE_TEXT_FLAG_MULTI_LINE.
    -->
    <!--
        android:minLines="2"
            It will initially display minimum 2 lines on EditText.

        android:maxLines="3"
            It will display maximum 3 lines on App interface.

        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>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.os.Bundle;
import android.app.Activity;
import android.widget.EditText;
import android.widget.Scroller;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the widgets reference from XML layout
        final EditText et = (EditText) findViewById(R.id.et);
        final EditText et2 = (EditText) findViewById(R.id.et2);

        /*
            This will provide Scroll option for EditText,
            but it will not show the scroll bar on EditText
        */
        et2.setScroller(new Scroller(getApplicationContext()));
        et2.setVerticalScrollBarEnabled(true);

        /*
            setMinLines (int minlines)
                Makes the TextView at least this many lines tall.
                Setting this value overrides any other (minimum) height
                setting. A single line TextView will set this value to 1.
         */
        // Set the minimum lines to display on EditText
        et2.setMinLines(2);

        /*
            setMaxLines (int maxlines)
                Makes the TextView at most this many lines tall.
                Setting this value overrides any other (maximum) height setting.
         */
        // Set the maximum lines to display on EditText
        et2.setMaxLines(2);
   }
}



More android examples