EditText with number keypad by default, but allowing alphabetic characters EditText with number keypad by default, but allowing alphabetic characters android android

EditText with number keypad by default, but allowing alphabetic characters


Define your EditText in your xml this way:

<EditText     android:id="@+id/txtOppCodigoCliente"    android:inputType="textVisiblePassword"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:maxLength="11"    android:hint="@string/strCodigoCliente"/>

output: android:inputType="textVisiblePassword"

A row of numbers and the rest of letters.


I would suggest a more Robust solution:

Present the user with the ability to choose the input type:

NEEDED FIXES

-better handling of showing/hiding the input selection buttons

-probably remove the suggestions when the input type is letter

enter image description here

Here is the activity:

package mobi.sherif.numberstexttextview;import android.os.Bundle;import android.app.Activity;import android.text.InputType;import android.view.View;import android.view.View.OnFocusChangeListener;import android.widget.EditText;public class MainActivity extends Activity {    EditText mEdit;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mEdit = (EditText) findViewById(R.id.input);        mEdit.setOnFocusChangeListener(new OnFocusChangeListener() {            @Override            public void onFocusChange(View view, boolean focused) {                findViewById(R.id.llmenu).setVisibility(focused?View.VISIBLE:View.GONE);            }        });    }    public void onNumbers(View v) {        mEdit.setInputType(InputType.TYPE_CLASS_NUMBER);    }    public void onLetters(View v) {        mEdit.setInputType(InputType.TYPE_CLASS_TEXT);    }}

Here is the activity's xml : activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <ScrollView        android:id="@+id/scrollview"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_alignWithParentIfMissing="true"        android:layout_above="@+id/llmenu" >        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content" >            <EditText                android:id="@+id/input"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:inputType="number" >                <requestFocus />            </EditText>        </LinearLayout>    </ScrollView>    <LinearLayout        android:id="@+id/llmenu"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:background="#000000"        android:padding="0dp" >        <Button            android:id="@+id/buttonnumbers"            android:layout_width="0dp"            android:onClick="onNumbers"            android:layout_height="match_parent"            android:layout_weight="1"            android:text="Numbers" />        <Button            android:id="@+id/buttonletters"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:onClick="onLetters"            android:text="Letters" />    </LinearLayout></RelativeLayout>


Here is what you are looking for:

Define your EditText in your xml this way:

<EditText    android:id="@+id/etTest"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:ems="10"    android:hint="@string/hint"></EditText>

And call following in your onCreate():

etTest = (EditText) findViewById(R.id.etTest);etTest.setRawInputType(InputType.TYPE_CLASS_NUMBER);// Note the "Raw"

Whenever you click into your EditText now, you can enter digits AND letters! (Digit keyboard is shown first)