5

I am creating an email form and would like to have text in an EditText that cannot be deleted. On the screenshot below, the To could not be deleted.

Cannot delete From - is this possible?

If anyone has suggestions on how to achieve the above, it would be great - Thanks.

My current code for the To EditText box:

<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="0dp"
    android:hint="@string/email_to" >
</EditText>

The problem is android:hint text dissappears when the user starts to text, and android:text can be deleted by the user.

How do we have text that cannot be deleted? Thanks.

Note:

Also, I would like to note that I have a method that clears text using a clear button - this works fine - but I am hoping that it would not delete the fixed text (If I got that implemented!).. Here`s the code for that:

private void clearForm(ViewGroup group)
{       
    for (int i = 0, count = group.getChildCount(); i < count; ++i) {
    View view = group.getChildAt(i);
    if (view instanceof EditText) {
        ((EditText)view).setText("");
    }

    if(view instanceof ViewGroup && (((ViewGroup)view).getChildCount() > 0))
        clearForm((ViewGroup)view);
}
}

SOLUTION:

Managed a roundabout way of doing this.. I created a TextView and EditText within a nested Linear Layout. I turned off the border in the EditText using android:background="#00000000".

I created an xml file in the drawable folder, and refered to this in the relevant linear layout like this: android:background="@drawable/customxml"

Community
  • 1
  • 1
Tom
  • 561
  • 8
  • 16
  • Is there any reason you need to store "To" in the EditText itself as opposed to in another view beside the EditText? – Dale Wilson Sep 30 '13 at 14:52
  • @DaleWilson No particular need - but I think It will look better to have in The EditText itself...I will try a separate TextView again to make sure – Tom Sep 30 '13 at 14:55
  • @DaleWilson When I have a seperate TextView it does not share the formatted bottom border of the EditText - although I will investigate how to share this border. I also think it would be more efficient to have it in the same box rather than nesting another linear layout to have the boxes side by side. – Tom Sep 30 '13 at 15:04
  • Does this answer your question? [How to prevent deleting the first character of edit text in Android](https://stackoverflow.com/questions/29966287/how-to-prevent-deleting-the-first-character-of-edit-text-in-android) – PPartisan Aug 02 '22 at 09:41

3 Answers3

3

You can do it by using Text Watcher listener. You can keep text in edittext by checking length of edittext.

For example editText.setText("To") // it mean have lenght 2

And Than in method afterTextChanged, check if text in editText is has been deleted (with check length of text in editText)

This is for complete example code:

    editText.setText("To");
    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 s) {
            if(s.length < 2){
               editText.setText("To")//set editext with "To" again like has been initialized 
               editText.setSelection(editText.getText().length)// to make cursor in end of text
            }
        }
     });
       

Hope this help!

2

To get the visual appearance you want, include a horizontal LinearLayout containing a text view and an EditView. Turn off the border around the EditView (there's an attribute that does that (I think it's android:shadowColor) ) Play around with margins and padding to get them to be adjacent to each other. Set the background color on the linear layout to put a border around the combined pair.

I wouldn't worry much about efficiency. You aren't nesting very deeply. The biggest challenge is going to be getting it to look like a single view.

Edit: Another thought. If that doesn't work, you could make the "To" a drawable, and set it using the android:drawableLeft attribute.

Dale Wilson
  • 9,166
  • 3
  • 34
  • 52
  • Managed a roundabout way of doing this.. the border could not be turned off using `android:shadowColor` but `android:background="#00000000"` worked. I created an xml file in the `drawable` folder, and refered to this in the relevant linear layout like this: `android:background="@drawable/customxml"` – Tom Sep 30 '13 at 16:51
  • It is better to use addTextChangedListener(TextWatcher watcher). – Khyati Vara Aug 13 '21 at 06:04
-1

Add TextView to the right side of EditText in LinearLayout with horizontal orientation or RelativeLayout with android:layout_toRightOf="@id/YourTextView"