Monday, March 21, 2016

android - EditText first letter capitalization

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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="16dp"
    tools:context=".MainActivity"
    android:background="#ccd1c5"
    >
    <!--
        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.
    -->
    <!--
        textCapSentences
            Can be combined with text and its variations to request capitalization of the first
            character of every sentence. Corresponds to TYPE_TEXT_FLAG_CAP_SENTENCES.
    -->
    <EditText
        android:id="@+id/et"
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine|textCapSentences"
        android:minLines="2"
        android:maxLines="3"
        android:background="#899e91"
        android:padding="5dp"
        android:layout_margin="10dp"
        />
    <EditText
        android:id="@+id/et_second"
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/et"
        android:background="#b18fba"
        android:padding="5dp"
        android:layout_margin="10dp"
        />
    <EditText
        android:id="@+id/et_third"
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/et_second"
        android:background="#90a7d4"
        android:padding="5dp"
        android:layout_margin="10dp"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

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


public class MainActivity extends AppCompatActivity {

    private Context mContext;
    private Activity mActivity;

    private RelativeLayout mRelativeLayout;
    private EditText mEditText;
    private EditText mEditTextSecond;
    private EditText mEditTextThird;


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

        // Get the application context
        mContext = getApplicationContext();

        // Get the activity
        mActivity = MainActivity.this;

        // Get the widgets reference from XML layout
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mEditText = (EditText) findViewById(R.id.et);
        mEditTextSecond = (EditText) findViewById(R.id.et_second);
        mEditTextThird = (EditText) findViewById(R.id.et_third);

        /*
            public void setInputType (int type)
                Set the type of the content with a constant as defined for inputType. This will
                take care of changing the key listener, by calling setKeyListener(KeyListener),
                to match the given content type. If the given content type is TYPE_NULL then a soft
                keyboard will not be displayed for this text view. Note that the maximum number of
                displayed lines (see setMaxLines(int)) will be modified if you change the
                TYPE_TEXT_FLAG_MULTI_LINE flag of the input type.
        */
        /*
            public static final int TYPE_TEXT_FLAG_CAP_SENTENCES
                Flag for TYPE_CLASS_TEXT: capitalize the first character of each sentence. This
                value is explicitly defined to be the same as CAP_MODE_SENTENCES. For example in
                English it means to capitalize after a period and a space (note that other languages
                may have different characters for period, or not use spaces, or use different
                grammatical rules). Of course, this only affects languages where there are
                upper-case and lower-case letters.

            Constant Value: 16384 (0x00004000)
        */
        // Programmatically enable the EditText to sentence first letter capitalization
        mEditTextSecond.setInputType(
                InputType.TYPE_CLASS_TEXT|
                        InputType.TYPE_TEXT_FLAG_CAP_SENTENCES
        );

        /*
            public static final int TYPE_TEXT_FLAG_CAP_WORDS
                Flag for TYPE_CLASS_TEXT: capitalize the first character of every word. Overrides
                TYPE_TEXT_FLAG_CAP_SENTENCES. This value is explicitly defined to be the same as
                CAP_MODE_WORDS. Of course, this only affects languages where there are
                upper-case and lower-case letters.

            Constant Value: 8192 (0x00002000)
        */
        // Programmatically enable the third EditText to word first letter capitalization
        mEditTextThird.setInputType(
                InputType.TYPE_CLASS_TEXT|
                        InputType.TYPE_TEXT_FLAG_CAP_WORDS
        );
    }
}