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;
}
}
});
}
Comments
-
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 about 11 yearsI want only by using only Editext, how we can change the soft keyboard input type number to alphabets
-
Tripaty Sahu about 11 yearsI 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 about 11 yearsHave you put android:inputType="number" in xml ?
-
Hardik Joshi about 11 yearsif you provide android:inputType="number" in xml then by default it only accept number not all.
-
Tripaty Sahu about 11 yearsIs there any other way to achieve my requirement
-
Tripaty Sahu about 11 yearsI want by using only Editext control, how we can change the soft keyboard input type number to alphabets
-
SKK about 11 yearsjust 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 over 8 yearsyou can use android:inputType="number" android:digits="0,1,2,3,4,5,6,7,8,9"