How to change input type Number to Text of EditText in android

23,059

You could use a toggle button to switch between number to text and vice versa.

in your layout xml:

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="number" >

    <requestFocus />
</EditText>

<ToggleButton
    android:id="@+id/toggleButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="InputType"
    android:textOff="Text"
    android:textOn="Number" />

Activity class:

EditText ed;
ToggleButton edtb;

//flag : used in the added code

static boolean flag = false;

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

    ed = (EditText) findViewById(R.id.editText1);
    edtb = (ToggleButton) findViewById(R.id.toggleButton1);

    edtb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if(isChecked)
            {
                ed.setInputType(InputType.TYPE_CLASS_TEXT);
            }
            else
            {
                ed.setInputType(InputType.TYPE_CLASS_NUMBER);
            }

        }
    });

  /* **Edited** Adding a way to achieve the same using EditText only. */

  ed.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if(flag)
            {
                ed.setInputType(InputType.TYPE_CLASS_TEXT);
                flag=false;
            }
            else
            {
                ed.setInputType(InputType.TYPE_CLASS_NUMBER);
                flag=true;
            }

        }
    });

}
Share:
23,059
Tripaty Sahu
Author by

Tripaty Sahu

I am an Android developer

Updated on March 08, 2020

Comments

  • Tripaty Sahu
    Tripaty Sahu about 4 years

    I have a requirement in my project,that by default the input type of the Edittext is Number.How we can give an option to the User to change the input type Number to Text/Alphabets.

    Thanks Tiru

  • Tripaty Sahu
    Tripaty Sahu about 11 years
    I want only by using only Editext, how we can change the soft keyboard input type number to alphabets
  • Tripaty Sahu
    Tripaty Sahu about 11 years
    I tried this way,but by default it is showing text, I need to show number by default.User can change it to Text type
  • Hardik Joshi
    Hardik Joshi about 11 years
    Have you put android:inputType="number" in xml ?
  • Hardik Joshi
    Hardik Joshi about 11 years
    if you provide android:inputType="number" in xml then by default it only accept number not all.
  • Tripaty Sahu
    Tripaty Sahu about 11 years
    Is there any other way to achieve my requirement
  • Tripaty Sahu
    Tripaty Sahu about 11 years
    I want by using only Editext control, how we can change the soft keyboard input type number to alphabets
  • SKK
    SKK about 11 years
    just a note: I feel using a toggle button increases user Experience as the user will have some visual telling him that he could change the setting. Happy coding.
  • chanu panwar
    chanu panwar over 8 years
    you can use android:inputType="number" android:digits="0,1,2,3,4,5,6,7,8,9"