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.