How to prevent deleting the first character of edit text in Android
Kotlin example. You can do like this:
editText.addTextChangedListener(object: TextWatcher {
override fun afterTextChanged(s: Editable?) {
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if (editText.length() < 5 || !editText.text.startsWith("+998 ")) {
editText.setText("+998 ")
editText.setSelection(editText.length());
}
}
})
You could place your EditText in a separate RelativeLayout in the main Layout that you have and add one TextView with text / just before it on the same line and leave in the EditText text only the "pictures" part. Something like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "/"/>
<EditText
android:id="@+id/etFolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="pictures"
android:ems="10"
android:layout_marginLeft="10dp" >
</RelativeLayout>
There are a few ways you can achieve this. First, you can simply have it so that if the EditText
block is empty, it is immediately repopulated with a "/" char. Alternatively, make it so that if the previous char
is /
, then prevent the user from deleting back. The code below is untested, so you may need to tweak it a little.
editText = (EditText) findViewById(R.id.editText);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (editText.getText().charAt(editText.length()-1) == '/') {
editText.append(" ");
}
//OR...
if (editText.length() == 0) {
editText.setText("/")
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
Edit: FWIW, if I were in your position I would personally opt for a solution like Gabriella's and apk's combined. However, as your question was specific, I have tried to answer it directly.
There may be an accepted answer here, but did not helped me at all, so for those looking for alternative way to do it heres the code.
My problem was, to prevent user to delete the first 3 characters in my EditText
which is for area code of phone number (+63)
public class TextListener implements TextWatcher {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (count < 3) {
etMobileNumber.setText("+63");
etMobileNumber.setSelection(count + 1);
}
}
}
//
// use it like this
//
etMobileNumber.addTextChangedListener(new TextListener());
This way user wont be able to delete/remove the first 3 characters on my EditText
and will always have a prefix at the start of the text.
Happy codings ! Cheers if you found this useful.