Reputation: 186
I generated this activity using the Android Studio Navigation Drawer preset, so the EditText is inside a fragment. What I'm trying to do is to create a notification after the "Send" button is pressed on the keyboard. When I click the button nothing happens, the onEditorAction is not called. Anyone knows how to solve this issue?
inflatedView = getLayoutInflater().inflate(R.layout.fragment_main, null);
EditText noteText = (EditText) inflatedView.findViewById(R.id.write_note);
noteText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
createNotification();
handled = true;
}
return handled;
}
});
fragment_main.xml
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/write_note"
android:background="@drawable/edit_text_outline"
android:layout_below="@+id/textView"
android:textSize="22sp"
android:textColor="#ffffffff"
android:textColorHint="#c8ffffff"
android:layout_marginTop="42dp"
android:inputType="textAutoCorrect|textCapSentences"
android:imeOptions="actionSend"
android:singleLine="true"
android:hint="@string/edit_text_hint" />
Upvotes: 0
Views: 1889
Reputation: 1
View rootView = inflater.inflate(R.layout.fragment_address, container, false);
editText = rootView.findViewById(R.id.et_country);
editText.setOnEditorActionListener(
new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_DONE ||
event != null &&
event.getAction() == KeyEvent.ACTION_DOWN &&
event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
{ if (event == null || !event.isShiftPressed()) { // the user is done typing. return true; // consume. } return false; // pass on to other listeners. } } );
Upvotes: 0
Reputation: 1568
do this way,please set "android:imeOptions="actionSend" in edittext.
noteText.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND)
{
createNotification();
handled = true;
}
return handled;
}
});
put this into xml,
<EditText
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionSend"
android:singleLine="true" />
This works for me. You can hide the soft keyboard after made the calculations using a code like this:
InputMethodManager imm= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
Upvotes: 1
Reputation: 237
use android:imeOptions="actionSend"
in xml of edittext and let us know if it helps
Upvotes: 0