14

This is one of my Edit text in the application

<EditText
        android:id="@+id/etFolder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="/pictures"
        android:ems="10" >

When it appear at first time it comes with "/pictures"

User can change the text and enter another word. but how to prevent deleting "/" of Edit text.

user can delete all other text but should not allow to delete first character.

How can I achieve this behavior ?

GreenCodes
  • 241
  • 1
  • 4
  • 11
  • What you tried till now??? – M D Apr 30 '15 at 11:17
  • I am new to programming.. I have thin a logic. but cannot write a code.. when textchange check it similar to '/' if not allow to delete..If it is similar to '/' does not allow to delete...but I cannot write a code for that :( – GreenCodes Apr 30 '15 at 11:20
  • Why do you want to do this?? I guess you want only text from that edittext. So you can simply take the text and append the text with "/" as first character. You get what you want. Text with first character "/". – Sajal Apr 30 '15 at 11:23
  • This might be of some help: [Put a constant text inside EditText which should be non-editable][1] [1]: http://stackoverflow.com/questions/14195207/put-constant-text-inside-edittext-which-should-be-non-editable-android – 4127157 Apr 30 '15 at 11:50

8 Answers8

7

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.

PPartisan
  • 8,173
  • 4
  • 29
  • 48
6

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>
Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
4

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.

ralphgabb
  • 10,298
  • 3
  • 47
  • 56
4

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());
            }
        }
    })
Kuvonchbek Yakubov
  • 558
  • 1
  • 6
  • 16
3

Based on ralphgabb's answer, this overcomes the problem of the caret ending up in the middle of the prefix when you do a rapid delete:

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) {
    }

    @Override
    public void afterTextChanged(Editable editable) {

        String prefix = "+63";

        int count = (editable == null) ? 0 : editable.toString().length();
        if (count < prefix.length()) {

            editText.setText(prefix);

            /*
             * This line ensure when you do a rapid delete (by holding down the
             * backspace button), the caret does not end up in the middle of the
             * prefix.
             */
            int selectionIndex = Math.max(count + 1, prefix.length());

            editText.setSelection(selectionIndex);
        }
    }
});
ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253
1

You can't achieve such a behavior for EditText

But you can make some work-arounds

Use TextWatcher in Android . So you can listen to the changes in the EditText

Here is a complete Example code snippet

Don Chakkappan
  • 7,397
  • 5
  • 44
  • 59
1

rather than doing this let the user enter whatever text he wants and when you call getText() attach "/" at start as below

String text = "/"+editText.getText();
amodkanthe
  • 4,345
  • 6
  • 36
  • 77
1

#KOTLIN_EXAMPLE

    editText.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                val prefix = "+91 "
                val count = s?.toString()?.length ?: 0
                if (count < prefix.length) {
                    if (s.toString() != prefix.trim()) {
                        editText.setText("$prefix$s")
                    } else {
                        editText.setText(prefix)
                    }
                    editText.setSelection(editText.length())
                }
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
        })

This way user won't be able to remove/delete the defined prefix. Moreover, the first entered character will also be appended in the EditBox.

#KOTLIN_EXAMPLE

#HappyCoding

Kartik
  • 161
  • 1
  • 5