I need to enter only hexadecimal characters to an editText in Android, but these characters must be entered in pairs, after each pair a space must automatically be inserted and then the next pair and so on, could someone tell me how to do it
To enter the hex characters I am using this code:
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE
|| event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
Log.d("", tv.getText().toString());
return true;
}
return false;
}
});
and the xml layout is like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/edittext_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:digits="0123456789ABCDEF"
android:imeOptions="actionDone"
android:inputType="textCapCharacters" />
One way to allow your to
EditText
only accept hexadecimal characters is through the properties:This would configure your
EditText
to only receive the characters configured inandroid:digits
.But what you want is that it also adds a space every two characters, to do this you can define an InputFilter , where it will evaluate if the character is alphanumeric and by means of a
REGEX
if it is included in the characters"0123456789ABCDEF"
, when detecting that you have written 2 allowed characters it will add a space for the following:This way you would get the desired behavior.