6

I have a layout with one textView and one EditText and a button. EditText is made as non editable in xml file. I want to make it enabled via code.

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name"
        android:id="@+id/textView12"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
         />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/etName"
        android:layout_below="@+id/textView12"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:editable="false"
        />
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/edit"
        android:onClick="edit"
        android:id="@+id/btnName"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
Cœur
  • 37,241
  • 25
  • 195
  • 267
Krishna
  • 129
  • 1
  • 2
  • 9

2 Answers2

13

Remove android:editable="false" this line from android xml file as this method is deprecated in android. and use below code to do so.

  • Make EditText non editable using java code

In onCreate of activity do the following after setContentView() method

EditText text = (EditText) findViewById(R.id.etName);
text.setTag(text.getKeyListener());
text.setKeyListener(null);
  • And make it editable using following code

    text.setKeyListener((KeyListener) textView.getTag());
    

Hope it'll help.

ELITE
  • 5,815
  • 3
  • 19
  • 29
1

hey instead of editable use android:enabled="false" and in code setEnabled(true)

Naveen Shriyan
  • 1,266
  • 1
  • 9
  • 15
  • let me know the feedback – Naveen Shriyan Mar 08 '16 at 14:36
  • Hi Naveen, thanks for your valuable suggestion. But your suggestion wont work as per my requirement. I want the edittext not edittable initally, on click of an Imagebutton it need to turn as editable. As per your suggesion, initially it will be editable on loading itself. – Krishna Mar 09 '16 at 05:23
  • try with this combination once, android:enabled="false" android:focusable="false" and in code setEnabled(true) – Naveen Shriyan Mar 09 '16 at 05:27