3

in my application EditText is there which is disable and I want to implement long press option on this edittext(while disable mode) which enable it and allowing to enter the character from softkey.

Example:-

Suppose initially I am allowing user to enter some number into the EditText. After some operation, I need to disable this EditText. Again if user want to change the number which previously he enter in the editText then first he need to long press on this editText. After doing long press on this editText, editText get enable and again user will able to change or retype the number. I need to do some operation before changing the number in the editText and during operation user not have any option to change the number in the editText.

Code:-

<EditText
        android:id="@+id/eTextBillNoFrmReturn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="2dp"
        android:layout_weight="7"
        android:clickable="true"
        android:background="@drawable/custom_edit_text"
        android:inputType="number" />

@Override
public boolean onLongClick(View v) {
    // do some operation
    return false;
}

but this code work only EditText is enable. Long press not work when EditText is disable.

Vinit ...
  • 1,409
  • 10
  • 37
  • 66
  • 1
    i think it should be possible with OnTouchListner see here http://stackoverflow.com/questions/10853117/can-i-make-a-button-appear-disabled-and-still-listen-for-clicks – Hardik Mar 27 '14 at 13:09
  • have you got solution? – Hardik Mar 28 '14 at 04:58
  • @Hardik ... no I not got the solution.. the link which u provided here is the problem which is for button. But in my case it is EditText. If I will change the background of editText so it looks like Disable then at that time user can also able to enter text. – Vinit ... Mar 28 '14 at 05:55
  • i want to point you Tal Kanel answer not accepted one just check it – Hardik Mar 28 '14 at 06:45

4 Answers4

0
Try this one.

@Override
public boolean onLongClick(View v) {
    // do some operation
btnname.setenable(true);
btnname.setfocusable(true);
    return false;
}
Sethu
  • 430
  • 2
  • 13
  • actually what you said before perform your long press the button will enable its rite or it ll be perform after btn click??? – Sethu Mar 28 '14 at 06:30
0

In case anyone stumbles across this old post like I did, I thought I would post an answer since I found some settings that worked well for my situation.

The OP was asking for a way to make the EditText function as if it is disabled yet still allow setting a long-click listener that can get called.

Set the edit text to be NOT focusable and set the input type to none. The input type to none will make it so that a cursor doesn't try to show up when clicking on it, so it looks a bit nicer than the cursor that flashes for a second with just the focusable set to false.

To do this in the XML:

<EditText
    android:id="@+id/txtYourEditTextID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:inputType="none" />

To do this in the code:

EditText txtYourEditText = (EditText)findViewById(R.id.txtYourEditTextID);
txtYourEditText.setFocusable(false);
txtYourEditText.setInputType(InputType.TYPE_NULL);

To do what OP is wanting, you will implement a long-click listener:

txtYourEditText.setOnLongClickListener(new View.OnLongClickListener {
    @Override
    public boolean onLongClick(View v) {
        ((EditText)v).setInputType(InputType.TYPE_CLASS_NUMBER);
        v.setFocusable(true);
        return true; //or return false if you do not want to consume the event
    }
});

Then on whatever the action is that you want to disable it again, call

txtYourEditText.setFocusable(false);
txtYourEditText.setInputType(InputType.TYPE_NULL);

Hopefully this will help someone who is trying to do something similar.

Mira_Cole
  • 763
  • 6
  • 13
-1

Have you tried to replace

return false;

by

return true;

on your onLongClick(View v) method ?

JossVAMOS
  • 300
  • 1
  • 5
  • 20
-1

Use the toggle button which will suite perfectly your case Toggle Button

Saf
  • 227
  • 3
  • 11
  • @SFN... I can not replace EditText with button or with Toggle Button...because in button user can not enter text.... – Vinit ... Mar 28 '14 at 04:09