3

I'm quite new to android development and when working on my company project, I encounter a problem. I need an Edittext with fixed suffix as part of the Edittext so that when user type in, the suffix will wrap to new line just like normal content of the Edittext, but user can not edit and select this suffix part.

I have search around and can't find a good solution for this problem. Some suggest to add a drawable contains text to right of edittext, but this solution will not make the suffix part wrap to new line when people type in.

Another possible solution is to handle text change for the edittext but this will lead to very complicated handle for the cursor of edittext(like handle text-hint and user selection gesture, ...)

So my question is: Is there anyone have implement something like this or can someone point me some directions to easily implement this feature for edittext.

0

2 Answers 2

3

Here is dirty solution to your problem.

Lets assume you have an EditText called mEditText, a String called SUFFIX, and a boolean addedSuffix:

 boolean addedSuffix = false;
 String SUFFIX = " my suffix";
 EditText mEditText = (EditText) findViewById(R.id.my_edit_text);

attach a textWatcher to your EditText

mEditText.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 the only text is the suffix
                if(s.toString().equals(SUFFIX)){
                    mEditText.setText(""); // clear the text
                    return;
                }

                // If there is text append on SUFFIX as long as it is not there
                // move cursor back before the suffix
                if(s.length() > 0 && !s.toString().contains(SUFFIX) && !s.toString().equals(SUFFIX)){
                    String text = s.toString().concat(SUFFIX);
                    mEditText.setText(text);
                    mEditText.setSelection(text.length() - SUFFIX.length());
                    addedSuffix = true; // flip the addedSuffix flag to true
                }
            }

            @Override
            public void afterTextChanged(Editable s) { 
                if(s.length() == 0){
                     addedSuffix = false; // reset the addedSuffix flag
                }
            }
        });

Like i said this is a quick and dirty solution, so this only adds the suffix when the user actually types into the EditText field. If you need it to be added before the user starts typing you can modify the logic to do so on your own.

Good luck and Happy Coding!

0

Please try this code on your edittext's onfocuschangedlistener. your suffix value added when your focuschangelistener not focused or focusing on other fields.

//sdur is my edittext name

sdur.setOnFocusChangeListener(View.OnFocusChangeListener { v, hasFocus ->


        if (!hasFocus) {
            if((!sdur.text.toString().endsWith("mins")&&(sdur.text.toString().isNotEmpty()))){
                var jj=sdur.text.toString()
                sdur.setText("$jj mins")
            }
           // code to execute when EditText loses focus
        }

});

Not the answer you're looking for? Browse other questions tagged or ask your own question.