Wednesday, September 28, 2016

android - Restrict EditText to single line

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#df425c"
    >
    <!--
        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. The constants here correspond to those defined by InputType.
        Generally you can select a single value, though some can be combined together as indicated.
        Setting this attribute to anything besides none also implies that the text is editable.

        Must be one or more (separated by '|') of the following constant values.

        text
            Just plain old text. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_NORMAL.
    -->
    <!--
        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.

            Must be an integer value, such as "100".

            This may also be a reference to a resource (in the form "@[package:]type:name") or
            theme attribute (in the form "?[package:][type:]name") containing a value of this type.

            This corresponds to the global attribute resource symbol maxLines.
    -->
    <EditText
        android:id="@+id/et"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|center_horizontal"
        android:textSize="25sp"
        android:padding="5dp"
        android:layout_marginTop="50dp"
        android:hint="Write some text here."
        android:maxLines="1"
        android:inputType="text"
        />
</android.support.design.widget.CoordinatorLayout>

2. Restrict EditText to single line programmatically

MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.widget.EditText;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private CoordinatorLayout mCLayout;
    private EditText mEditText;


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

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        mEditText = (EditText) findViewById(R.id.et);

        // Restrict edit text to write only single line

        // Set EditText maximum lines
        mEditText.setMaxLines(1);

        /*
            setInputType
                void setInputType (int type)
                Set the type of the content with a constant as defined for inputType.

                text
                    Just plain old text. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_NORMAL.
        */
        // Set EditText input type
        mEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);
    }
}