1

I have an EditText and I want to catch the event when the user is using "Done" or "Enter". Currently, I'm testing on an emulator with Pixel API 26

I've tried many solutions found on StackOverFlow such as adding setSingleLine or editing the XML with

android:singleLine="true"
android:inputType="text"
android:maxLines="1""

and nothing works. I really don't know what is the problem.

This is the XML:

<EditText
    android:id="@+id/input_search"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerVertical="true"
    android:layout_toEndOf="@+id/ic_magnify"
    android:background="@null"
    android:hint="Enter Address, City or Zip Code"
    android:imeOptions="actionGo|actionSearch|actionNext|actionSend"
    android:singleLine="true"
    android:inputType="text"
    android:maxLines="1"
    android:textColor="#000"
    android:textSize="15sp" /> 

In my MapFragment class, I'm obtaining the reference to the EditBox inside the "onCreateView" function:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View v = inflater.inflate(R.layout.activity_maps, container,
                false);

    mSearchText = v.findViewById(R.id.input_search);
    mSearchText.setSingleLine();
    init();

    // Hiding the action bar
    ((AppCompatActivity) getActivity()).getSupportActionBar().hide();

    return v;
}

and then in the "init" function, I'm doing this:

private void init() {

    Log.d("Yolo2", "Before");

    //Search field
    mSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
            if(actionId == EditorInfo.IME_ACTION_SEARCH
                || actionId == EditorInfo.IME_ACTION_DONE
                || keyEvent.getAction() == KeyEvent.ACTION_DOWN
                || keyEvent.getAction() == KeyEvent.KEYCODE_ENTER){
                    Log.d("Yolo2", "Yes");
                    //execute our method for searching
                    geoLocate();
                }

            Log.d("Yolo2", "No");
            return false;
        }
    });
}

I've tried many other events and none of them worked. I'm getting nothing from the debug logs. I've tried on key change events to catch and key typed inside the EditText.

Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
Ondskan
  • 31
  • 5
  • 1
    have you tried using textWatchers ?, I believe your requirements will work better with using those. – Umair Oct 30 '19 at 09:54
  • Try leaving only one action option`android:imeOptions="actionDone"` or `android:imeOptions="actionNext"` but then don't forget to add `android:nextFocusForward="@+id/your_next_focused_view"` – Dmitrii Leonov Oct 30 '19 at 10:04
  • Can you link to some of the solutions that didn't work, so we know what not to suggest as answers? – Haem Oct 30 '19 at 10:19
  • I followed this tutorial: https://www.youtube.com/watch?v=MWowf5SkiOE Which leads to the source code here: https://github.com/mitchtabian/Google-Maps-Google-Places/tree/99301af3e4bcb0ecc9b87da121af1b7fb7b17b8c Besides that I've a few solutions such as: https://stackoverflow.com/questions/53498009/handle-edittext-key-change-event-with-input-type-text https://stackoverflow.com/questions/8063439/android-edittext-finished-typing-event and many more. – Ondskan Oct 30 '19 at 11:39
  • @Umair I've tried it but didn't work also. I really don't know what's going on. Maybe the problem is with something else? – Ondskan Oct 30 '19 at 11:44
  • @DmitriiLeonov I've also tried your solution with no success. – Ondskan Oct 30 '19 at 11:44
  • @Ondskan maybe you didn't implemented it correctly :). Can you share your that piece of code ? – Umair Oct 30 '19 at 12:00
  • I'm sorry, I can't share the whole project. If there is any missing code that might be related let me know and I will share it. – Ondskan Oct 30 '19 at 12:15

4 Answers4

1

Thank you all for helping me with this issue. I've made a mistake by using this code in the MapFragment class when I should have used it on the MapActivity. My code works now just fine and all your answers are probably correct too.

Ondskan
  • 31
  • 5
0

Remove your if condition and give try

 mSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
            geoLocate();
            return true;
        }
    });
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
0

Try with android:imeOptions="actionDone" and check with below code. First handle KeyEvent and then action.

mSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
        if (keyEvent != null && keyEvent.getAction() != KeyEvent.ACTION_DOWN)
            return false;

        if(actionId == EditorInfo.IME_ACTION_DONE
            || actionId == EditorInfo.IME_NULL) {
                Log.d("Yolo2", "Yes");
                //execute our method for searching
                geoLocate();
        }

        Log.d("Yolo2", "No");
        return false;
    }
});
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
  • Same as before. No log and nothing happens when I'm trying to breakpoint on the 'If' statements. – Ondskan Oct 30 '19 at 11:38
  • `mSearchText` really attach to `input_search`? Could you please add more details? – Md. Asaduzzaman Oct 30 '19 at 11:40
  • ` View v = inflater.inflate(R.layout.activity_maps, container, false); mSearchText = v.findViewById(R.id.input_search); ' I don't know how to write code in the comments here so I've added the entire 'OnCreateView' function to the original post – Ondskan Oct 30 '19 at 11:47
0

Please try this

    mSearchText = v.findViewById(R.id.input_search);
mSearchText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_SEND) {
            geoLocate();
            handled = true;
        }
        return handled;
    }
});
Geek
  • 1,510
  • 14
  • 17
  • Make sure you use `android:imeOptions="actionSend"` or only one imeOptions in xml at a time and replace the same in java code. It will definitely work. – Geek Oct 30 '19 at 13:01