0

I been trying to get back my EditText back to editable programmatically but it simply does not work.

I looked at several threads and everybody is saying the same thing, but for whatever reason, it does not work for me.

I looked int this, but no success

Cannot change EditText back to being editable

Can anyone spot why my EditText never goes back to editable again? Once I disabled it and try to re-enabled it, the soft key never shows up again. Here's the code snap:

boolean enabled = true;
final Button button = (Button) findViewById(R.id.bnt3);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        enabled = !enabled;
        EditText editView = (EditText) findViewById(R.id.popup_dlg_edit_text_id);
        if (enabled) {
            //BUG: this code never makes my EditText editable again :(
            editView.setFocusable(true);
            editView.setEnabled(true);
            editView.setCursorVisible(true);
            editView.setFocusable(true);
            editView.setFocusableInTouchMode(true);
            editView.setClickable(true);
        } else {

            editView.setFocusable(false);
            editView.setEnabled(false);
            editView.setCursorVisible(false);
            editView.setKeyListener(null);
            editView.setBackgroundColor(Color.TRANSPARENT);
        }
    }
});

my layout

<EditText
    android:id="@+id/popup_dlg_edit_text_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:backgroundTint="@android:color/holo_green_light"
    android:hint="@string/app_name"
    android:inputType="textNoSuggestions"
    android:maxLines="1"
    android:textSize="@dimen/default_font_small" />
Ümañg ßürmån
  • 9,695
  • 4
  • 24
  • 41
gmmo
  • 2,577
  • 3
  • 30
  • 56

2 Answers2

0

For change disable/enable programmatically you can use:

button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        enabled = !enabled;
        EditText editView = (EditText) findViewById(R.id.popup_dlg_edit_text_id);
        if (enabled) {
            editView.setInputType(InputType.TYPE_CLASS_TEXT)
        } else {
            editView.setInputType(InputType.TYPE_NULL)
        }
    }
});

EDIT: Also you just can remove 2 line of you code and it will work correctly:

//just delete them
editView.setKeyListener(null);
editView.setBackgroundColor(Color.TRANSPARENT);

If you really want to do you EditText transparent when it disabled, just set visibility View.INVISIBLE/View.GONE and View.VISIBLE when enable.

punchman
  • 1,350
  • 1
  • 13
  • 23
0

first bind your view and listen for the button clicks I used butterknife for that. so in one line you can do it like this

@OnClick(R.id.test_btn)
void onChangeEditTextState() {
    editText.setEnabled(!editText.isEnabled());
}

to hide the view use setVisibility not a transparent backgroun

editText.setVisibility(editText.isEnabled()? View.VISIBLE:View.INVISIBLE);
beshoy samy
  • 144
  • 2
  • 8