-1

I want to Edit Text but only on onClick event and soon as it finishes the cursor visibility should be false again like before. Only onClick event would show cursor and editing enable as soon as done typing and press keyboard action button it should make the cursor visibility false again

Here is Xml part

<EditText
            android:id="@+id/pname"
            android:textStyle="bold"
            android:layout_toRightOf="@+id/profileimg"
            android:inputType="none"
            android:layout_marginBottom="15dp"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="15dp"
            android:textAllCaps="true"
            android:cursorVisible="false"
            android:background="@android:color/transparent"
            android:hint="@string/nickname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLength="10"
            android:clickable="true"
            android:onClick="onClick"/>

This is Coding part:

pname = (EditText) findViewById(R.id.pname);

pname.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         pname.setInputType(0x0000006);
         pname.setCursorVisible(true);
     }
});
/*pname.setOnFocusChangeListener(new View.OnFocusChangeListener() {
     @Override
     public void onFocusChange(View v, boolean hasFocus) {
         if(hasFocus){
             pname.setCursorVisible(false);
         } else {
             pname.setCursorVisible(false);
         }
     }
});
*/

pname.setOnEditorActionListener(new EditText.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER || actionId ==EditorInfo.IME_ACTION_DONE) {
            if (!event.isShiftPressed()) {
                pname.setCursorVisible(false);
                return true; // consume.
            }
        }
        return false;
    }
});

Problem/Error: It seems as soon as I am done typing and press keyboard action done key my activity stops working unfortunately

Exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.KeyEvent.getKeyCode()' on a null object reference at packagename.MainActivity$2.onEditorAction(MainActivity.java:‌​69) line no 69

Cœur
  • 37,241
  • 25
  • 195
  • 267
SameerKhan1406
  • 289
  • 5
  • 18
  • _activity stops working unfortunately_ Is there an exception ? I am not sure I understand those explication. PLease try to explain this a but more. – AxelH Dec 08 '16 at 08:41
  • @AxelH java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.KeyEvent.getKeyCode()' on a null object reference at packagename.MainActivity$2.onEditorAction(MainActivity.java:69) line no 69 is "If" Condition and The problem I wrote is the pop up i am getting in my android mobile screen Unfortunately your app stop working Got it? – SameerKhan1406 Dec 08 '16 at 08:48
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – AxelH Dec 08 '16 at 08:50
  • @AxelH Wow Brilliant ! I am asking where I am getting null value and why when I am editting text into it. First read properly and then answer I know this is a null pointer exception but in my CASE WHERE????? – SameerKhan1406 Dec 08 '16 at 08:51
  • @AxelH Seriously! I know but why ???? I know Its not taking my action key but why ? – SameerKhan1406 Dec 08 '16 at 08:56

1 Answers1

1

You are not checking the KeyEvent received but the doc prevent you :

KeyEvent: If triggered by an enter key, this is the event; otherwise, this is null.

So you need to check if this is not null to prevent exception.

Here is the Source

AxelH
  • 14,325
  • 2
  • 25
  • 55
  • I dont know see this I have used a code from here http://stackoverflow.com/questions/8063439/android-edittext-finished-typing-event – SameerKhan1406 Dec 08 '16 at 09:04
  • But you check first the key, not the action. Maybe the event is only null if `actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE`. Meaning that this is doing this check like this. But that is not the point. The doc tell you it can be `null`, you get a `NullPointerException` ... so just check if it is `null` to prevent that. You can always see when it is happening to understand but that is not the point here – AxelH Dec 08 '16 at 09:10
  • Yeah I put a breakline and debug the app event is coming null now tell me how to fix it – SameerKhan1406 Dec 08 '16 at 09:15
  • Ok thanks I got it Let me try if done then come back and verify your answer thanks – SameerKhan1406 Dec 08 '16 at 09:17
  • OK did it but after action done button clicked its not going down again like it was happenning previously – SameerKhan1406 Dec 08 '16 at 09:27
  • @SameerKhan1406 This is not clear, please try to explain the problem better. What is that mean _not going down again_ ? Your requirements are not clear from the beginning... – AxelH Dec 08 '16 at 09:29
  • Keyboard is not going going down as soon as I am finish typing and tapping Action Button I put this condition : if (event!=null && event.getKeyCode() != KeyEvent.KEYCODE_ENTER || actionId != EditorInfo.IME_ACTION_DONE ) { return false; } then : else if(actionId==EditorInfo.IME_ACTION_DONE || event==null || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) – SameerKhan1406 Dec 08 '16 at 09:33
  • @SameerKhan1406 That is the first time you are talking about hidding the vitrual keyboard here. For this, simply look at [this question](http://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard) – AxelH Dec 08 '16 at 09:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/130091/discussion-between-sameerkhan1406-and-axelh). – SameerKhan1406 Dec 08 '16 at 09:46
  • Follow up Discussion Please – SameerKhan1406 Dec 08 '16 at 09:54