Reputation: 1158
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.
Upvotes: 1
Views: 1534
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
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
Reputation: 417
You can use this library ,i have worked
https://github.com/gotokatsuya/ParkedTextView
Upvotes: 0