Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put constant text inside EditText which should be non-editable - Android

I want to have constant text inside editText like:

http://<here_user_can_write> 

User should not be able to delete any chars from "http://", I searched about this and found this:

editText.setFilters(new InputFilter[] {     new InputFilter() {         public CharSequence filter(CharSequence src, int start,             int end, Spanned dst, int dstart, int dend) {             return src.length() < 1 ? dst.subSequence(dstart, dend) : "";         }     } });  

but I don't know whether it restricts user to not delete any chars from start to end limit. I also could not understand use of Spanned class.

One way would be a good choice if we can put a TextView inside EditText but I don't think it is possible in Android since both are Views, is it possible?

like image 603
Shrikant Ballal Avatar asked Jan 07 '13 11:01

Shrikant Ballal


People also ask

How do you make text not editable on android?

android:editable="false" should work, but it is deprecated, you should be using android:inputType="none" instead. Alternatively, if you want to do it in the code you could do this : EditText mEdit = (EditText) findViewById(R.

How do I limit text editing?

This example demonstrate about How to limit text length of EditText in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

Can you set text in EditText?

In android, we can set the text of EditText control either while declaring it in Layout file or by using setText() method in Activity file.

How do you make EditText not editable and clickable?

For the above requirement the solution in XML is android:editable="false" but I want to use this in Java. et. setKeyListener(null); It makes the EditText not EDITABLE but at the same time it makes it non clickable as well.


1 Answers

Did u try this method?

final EditText edt = (EditText) findViewById(R.id.editText1);  edt.setText("http://"); Selection.setSelection(edt.getText(), edt.getText().length());   edt.addTextChangedListener(new TextWatcher() {          @Override         public void onTextChanged(CharSequence s, int start, int before, int count) {             // TODO Auto-generated method stub          }          @Override         public void beforeTextChanged(CharSequence s, int start, int count,                 int after) {             // TODO Auto-generated method stub          }          @Override         public void afterTextChanged(Editable s) {             if(!s.toString().startsWith("http://")){                 edt.setText("http://");                 Selection.setSelection(edt.getText(), edt.getText().length());              }          }     }); 
like image 146
Rajitha Siriwardena Avatar answered Oct 13 '22 05:10

Rajitha Siriwardena