How to Replicate Android:Editable="False" in Code

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:

  1. Add the design library to your dependecies

     dependencies {
    implementation "com.google.android.material:material:1.2.1"
    }
  2. Use TextInputEditText in conjunction with TextInputLayout

     <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


  1. In your root layout don't forget to add xmlns:app="http://schemas.android.com/apk/res-auto"

  2. 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.

Sample Image



Related Topics



Leave a reply



Submit