1

I want to put some custom text on EditText . Which would be non editable And always appear on edit text.When we add something on edit text my non editable text should move forward.

Something like this should appear by default

Non editable Text

And when i type something on edit text it should appear like this

Hello World Non editable Text

Cactus
  • 27,075
  • 9
  • 69
  • 149
Nouman Ahmad
  • 13
  • 1
  • 3
  • There is a programmatical approach to this. I'll write up an answer in a bit once I figure it out. But here is another way to do it: http://stackoverflow.com/questions/17517278/editbox-hint-always-show-hint – Michael Yaworski Nov 20 '13 at 01:02
  • could you please take a look at my answer and see if it works for you. Leave a comment with some feedback. – Michael Yaworski Nov 20 '13 at 20:51

2 Answers2

1

Maybe you can do this with addTextChangedListener with your EditText.
But be carefull:

This method is called to notify you that, somewhere within s, the text has been
changed. It is legitimate to make further changes to s from this callback, but
be careful not to get yourself into an infinite loop, because any changes you
make will cause this method to be called again recursively. (You are not told
where the change took place because other afterTextChanged() methods may already
have made other changes and invalidated the offsets  

(As you can see here: https://stackoverflow.com/a/10862398/2668136)

You need to read these answers:

  1. Put constant text inside EditText which should be non-editable - Android
  2. Android java : Update same EditText in textChanged event
  3. Access edittext from textwatcher

I suppose you can getLength() of the user's input and add your String at the end of your EditText.
Hope this helpful.

Community
  • 1
  • 1
Blo
  • 11,903
  • 5
  • 45
  • 99
1

The text of the EditText is "(hint)" <-- this is important because of the lengths I use

This is the code. However, the only problem seems to be that when the user backspaces, it will not put the cursor in the correct place. You can probably fix that witht the setSelection(int) lines. I'll make an update if I figure it out.

Note: all of the Toasts are just tests so you know what is happening with the code.

String pastText = ""; // represents the text BEFORE the text is changed each time
EditText et;

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

    et = (EditText)findViewById(R.id.et);

    // run this code every time the EditText String is changed
    et.addTextChangedListener(new TextWatcher()
    {
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            // length of the String in the EdiText
            int length = et.getText().toString().length(); 

            // is the String of the EditText from first position to length - 6 position
            // (take away the non-editable text)
            String editableText = et.getText().toString().substring(0, length - 6);

            int cursor = et.getSelectionStart(); // where the cursor currently is

            // minSelection is the second position number of the non-editable
            // text in the EditText. It's the second position because the 
            // cursor needs to be BEFORE it, and the comparison will be 
            // if the cursor is LESS than minSelection.
            // so it's the length, subtract 5, because the non-editable text
            // is 6 chars long. Ex: if the EditText was "test(hint)" then
            // minSelection would be the position (as int) of 'h' in the String
            int minSelection = length-5;

            // if the user has his cursor BEFORE the non-editable text
            if (cursor < minSelection)
                Toast.makeText(getApplicationContext(), "good to type", 0).show();
            else { // otherwise (he can't edit)
                Toast.makeText(getApplicationContext(), "can't type", 0).show();
                int currentCursor = et.getSelectionStart(); // get where the cursor is
                et.setText(pastText); // set the text to what it used to be
                et.setSelection(currentCursor-1); // set cursor where it previously was
            }

            Toast.makeText(getApplicationContext(), "past text: " + pastText, 0).show();

            Toast.makeText(getApplicationContext(), 
                           "text: " + editableText + '\n' + '\n' 
                           + "cursor: " + cursor + '\n' + '\n' 
                           + "minSelection: " + minSelection, 0).show();
        }

        @Override public void afterTextChanged(Editable s) {} 

        @Override 
        public void beforeTextChanged(CharSequence s, int start, int count, int after) 
        { 
            // in the method beforeTextChanged, set pastText to be the text
            // of the EditText before the text is changed
            pastText = et.getText().toString(); 
        }
    });
}
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97