How to replicate android:editable=false in code?
I think an InputFilter
that rejects all changes is a good solution:
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) : "";
}
}
});
Edit: dlazar suggested (below) to change the return
to dst.subSequence(dstart, dend)
to overcome behavior that removes words.
How to set editable true/false EditText in Android programmatically?
I did it in a easier way , setEditable and setFocusable false. but you should check this.
How to replicate android:editable="false" in code?
EditText non editable
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.id.yourid);
mEdit.setEnabled(false);
This is also a viable alternative :
EditText mEdit = (EditText) findViewById(R.id.yourid);
mEdit.setKeyListener(null);
If you're going to make your EditText
non-editable, may I suggest using the TextView
widget instead of the EditText
, since using a EditText seems kind of pointless in that case.
EDIT: Altered some information since I've found that android:editable
is deprecated, and you should use android:inputType="none"
, but there is a bug about it on android code; So please check this.
Make EditText ReadOnly
Please use this code..
Edittext.setEnabled(false);
restrict edittext to single line
Use android:maxLines="1"
and android:inputType="text"
You forgot the android:maxLines attribute. And refer for android:inputType With your example, below will give this result:
<EditText
android:id="@+id/searchbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:inputType="text"
android:scrollHorizontally="true"
android:ellipsize="end"
android:layout_weight="1"
android:layout_marginTop="2dp"
android:drawablePadding="10dp"
android:background="@drawable/edittext"
android:drawableLeft="@drawable/folder_full"
android:drawableRight="@drawable/search"
android:paddingLeft="15dp"
android:hint="search...">
</EditText>
Android EditText inputType=none doesn't work, becomes textMultiLine
I had the same problem: defining the input type via xml did not work.
Then, to fix it, I set the input type programatically:
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState)
{
...
editText1= (EditText) super.getActivity().findViewById(R.id.editText1);
editText1.setInputType(InputType.TYPE_NULL); // none...
...
}
That works for me.
How to make EditText that cannot be editted?
editText.setFocusable(false);
editText.setClickable(false);
the two property which turn EditText to non focusable and unclickable which leads to a non editable Edittext
Allow multi-line in EditText view in Android?
By default all the EditText
widgets in Android are multi-lined.
Here is some sample code:
<EditText
android:inputType="textMultiLine" <!-- Multiline input -->
android:lines="8" <!-- Total Lines prior display -->
android:minLines="6" <!-- Minimum lines -->
android:gravity="top|start" <!-- Cursor Position -->
android:maxLines="10" <!-- Maximum Lines -->
android:layout_height="wrap_content" <!-- Height determined by content -->
android:layout_width="match_parent" <!-- Fill entire width -->
android:scrollbars="vertical" <!-- Vertical Scroll Bar -->
/>
How to make EditText not editable through XML in Android?
Use this simple code:
textView.setKeyListener(null);
It works.
Edit : To add KeyListener
later, do following
1 : set key listener to tag of textView
textView.setTag(textView.getKeyListener());
textView.setKeyListener(null);
2 : get key listener from tag and set it back to textView
textView.setKeyListener((KeyListener) textView.getTag());
Edit text Password Toggle Android
(updated for AndroidX)
Since the Support Library v24.2.0. you can achivie this very easy
What you need to do is just:
Add the design library to your dependecies
dependencies {
implementation "com.google.android.material:material:1.2.1"
}Use
TextInputEditText
in conjunction withTextInputLayout
<com.google.android.material.textfield.TextInputLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/etPasswordLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true">
<android.support.design.widget.TextInputEditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password_hint"
android:inputType="textPassword"/>
</com.google.android.material.textfield.TextInputLayout>
passwordToggleEnabled
attribute will make the password toggle appear
In your root layout don't forget to add
xmlns:app="http://schemas.android.com/apk/res-auto"
You can customize your password toggle by using:
app:passwordToggleDrawable
- Drawable to use as the password input visibility toggle icon.app:passwordToggleTint
- Icon to use for the password input visibility toggle.app:passwordToggleTintMode
- Blending mode used to apply the background tint.
More details in TextInputLayout documentation.
Related Topics
How to Run a Service Every Day at Noon, and on Every Boot
How to Filter Listview Data When Typing on Edittext in Android
How to Disable Status Bar Click and Pull Down in Android
Classloader Referenced Unknown Path: /Data/App/
How to Get Contacts from Native Phonebook in Android
Streaming Audio from a Url in Android Using Mediaplayer
How to Call Rest API from an Android App
How to Mask an Edittext to Show the Dd/Mm/Yyyy Date Format
Getting "Debuggable" Value of Androidmanifest from Code
Error: Configuration with Name 'Default' Not Found in Android Studio
How to Change Style of a Default Edittext
Glass Voice Command Nearest Match from Given List
Multiple Datepickers in Same Activity
Apache Http Connection with Android 6.0 (Marshmallow)
Retrieve String Out of Addvalueeventlistener Firebase