riadrifai
riadrifai

Reputation: 1158

How to add a constant uneditable text at the end of editText android?

I am trying to implement a sign up screen, where I have two ediTexts inside two TextInputLayouts (one for email and one for password). However, for email, I want to have a constant text for the domain. Is there any way I could set the text to remain at the end of the editText or any better suggestion? Email here is the hint, I want the user to be able to type at the start only, with the @example.com remaining constant.

enter image description here

Upvotes: 1

Views: 1534

Answers (3)

Abbas
Abbas

Reputation: 3331

So here's the updated solution.

    emailEditText.setText("@gmail.com");
    emailEditText.setSelection(0);
    emailEditText.requestFocus();
    emailEditText.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE:

                    Layout layout = ((EditText) v).getLayout();
                    float x = event.getX() + emailEditText.getScrollX();
                    offset = layout.getOffsetForHorizontal(0, x);
                    maxOffset = emailEditText.getText().toString().indexOf("@");

                    break;

                case MotionEvent.ACTION_UP:

                    if(offset > maxOffset && maxOffset >= 0) {

                        emailEditText.setSelection(maxOffset);
                    }
                    else if (offset >= 0) {
                        emailEditText.setSelection(offset);
                    }

                    event.setAction(MotionEvent.ACTION_CANCEL);

                    showSoftKeyBoard(emailEditText);
            }
            return false;
        }
    });

    public void showSoftKeyBoard(View focusedView) {
    if(getCurrentFocus() != null) {
        ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
                .showSoftInput(focusedView, InputMethodManager.SHOW_IMPLICIT);
    }
}

Finally a Disclaimer... you can move around the cursor but there is no holder that you usually see at the bottom of the cursor.

Upvotes: 0

manojs001
manojs001

Reputation: 13

You can either try one of these:

1) In a horizontal LinearLayout, put your textInputLayout (width=wrap_content) and a textView (width=0dp & weight=1) containing the domain name. Whenever the email edittext receives focus, show the textview else hide it.

2) Add a text watcher to the edittext and onAfterTextChanged method add the domain name to the string. Here you have to check whether the edittext's string ends with the domain name or not... if not then only add the domain name else do not add it. Personally, this is not an elegant solution as cursor wont be at expected position after text changes.

Thanks

Upvotes: 1

Rathod Vijay
Rathod Vijay

Reputation: 417

You can use this library ,i have worked

https://github.com/gotokatsuya/ParkedTextView

Upvotes: 0

Related Questions