Android setOnEditorActionListener() doesn't fire - java

I'm trying to set a listener to EditText when enter button will be pressed.But it didn't fire at all. I tested this on LG Nexus 4 with Android 4.2.2. setOnEditorActionListener works on Amazon Kindle Fire with Android 2.3 and setImeActionLabel works nowhere! I also can't set text for Enter button.Here is code:
mEditText.setImeActionLabel("Reply", EditorInfo.IME_ACTION_UNSPECIFIED);
mEditText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
Log.d("TEST RESPONSE", "Action ID = " + actionId + "KeyEvent = " + event);
return true;
}
});
What am I doing wrong? How can I fix this?

You can use TextWatcher.
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.charAt(s.length() - 1) == '\n') {
Log.d("TAG", "Enter was pressed");
}
}
});

Make sure that you have an IME_ACTION set in the layout file:
<EditText
android:id="#+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/search_hint"
android:inputType="text"
android:imeOptions="actionSend" />
For a full explanation, see http://developer.android.com/guide/topics/ui/controls/text.html.

What worked for me is this,I have added this below line to EditText
android:imeOptions="actionSend"
this line makes keyboard which pops up when clicked on edit text has send button instead of search
in the setOnEditorActionListener you override following method looking for action send
#Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
//implement stuff here
}
}

In the xml file add tag android:inputType="text" to the EditText

I use the following code
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/ic_magnify"
android:layout_centerVertical="true"
android:textSize="15sp"
android:textColor="#000"
android:id="#+id/input_search"
android:inputType="text"
android:background="#null"
android:hint="Enter Address, City or Zip Code"
android:imeOptions="actionSearch"/>
mSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, 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){
geoLocate();
}
return false;
}
});

You can try this to use the OnKeyListener
editText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int actionId, KeyEvent event) {
Log.d("TEST RESPONSE", "Action ID = " + actionId + "KeyEvent = " + event);
return false;
}
});

I got it, it works on Edittext in an Activity, but not in a Fragement.
<EditText
android:id="#+id/mEditText"
android:imeOptions="actionSearch"
android:imeActionLabel="搜索"
android:inputType="text"
android:singleLine="true"
android:textSize="18dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:textColor="#color/black"
android:layout_marginTop="7dp"
android:layout_marginBottom="7dp"
android:shadowColor="#4b000000"
android:shadowDy="1"
android:shadowDx="1"
android:shadowRadius="1"
android:cursorVisible="true"
android:textCursorDrawable="#null"
android:gravity="center_vertical|left"
android:background="#color/transparent"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
tools:ignore="UnusedAttribute">
<requestFocus/>
</EditText>
mEditText.setImeActionLabel("搜索", EditorInfo.IME_ACTION_SEARCH);
mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
MToastUtil.show("搜索");
}
return false;
}
});

make sure:
inputType = "text";
imeOptions = "actionSend";

After some little more searching - this was the solution for me (works also in a fragment):
Just remove the imeAction

Related

OnEditorActionListener of TextView doesn't get triggered [duplicate]

I'm trying to set a listener to EditText when enter button will be pressed.But it didn't fire at all. I tested this on LG Nexus 4 with Android 4.2.2. setOnEditorActionListener works on Amazon Kindle Fire with Android 2.3 and setImeActionLabel works nowhere! I also can't set text for Enter button.Here is code:
mEditText.setImeActionLabel("Reply", EditorInfo.IME_ACTION_UNSPECIFIED);
mEditText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
Log.d("TEST RESPONSE", "Action ID = " + actionId + "KeyEvent = " + event);
return true;
}
});
What am I doing wrong? How can I fix this?
You can use TextWatcher.
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.charAt(s.length() - 1) == '\n') {
Log.d("TAG", "Enter was pressed");
}
}
});
Make sure that you have an IME_ACTION set in the layout file:
<EditText
android:id="#+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/search_hint"
android:inputType="text"
android:imeOptions="actionSend" />
For a full explanation, see http://developer.android.com/guide/topics/ui/controls/text.html.
What worked for me is this,I have added this below line to EditText
android:imeOptions="actionSend"
this line makes keyboard which pops up when clicked on edit text has send button instead of search
in the setOnEditorActionListener you override following method looking for action send
#Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
//implement stuff here
}
}
In the xml file add tag android:inputType="text" to the EditText
I use the following code
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/ic_magnify"
android:layout_centerVertical="true"
android:textSize="15sp"
android:textColor="#000"
android:id="#+id/input_search"
android:inputType="text"
android:background="#null"
android:hint="Enter Address, City or Zip Code"
android:imeOptions="actionSearch"/>
mSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, 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){
geoLocate();
}
return false;
}
});
You can try this to use the OnKeyListener
editText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int actionId, KeyEvent event) {
Log.d("TEST RESPONSE", "Action ID = " + actionId + "KeyEvent = " + event);
return false;
}
});
I got it, it works on Edittext in an Activity, but not in a Fragement.
<EditText
android:id="#+id/mEditText"
android:imeOptions="actionSearch"
android:imeActionLabel="搜索"
android:inputType="text"
android:singleLine="true"
android:textSize="18dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:textColor="#color/black"
android:layout_marginTop="7dp"
android:layout_marginBottom="7dp"
android:shadowColor="#4b000000"
android:shadowDy="1"
android:shadowDx="1"
android:shadowRadius="1"
android:cursorVisible="true"
android:textCursorDrawable="#null"
android:gravity="center_vertical|left"
android:background="#color/transparent"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
tools:ignore="UnusedAttribute">
<requestFocus/>
</EditText>
mEditText.setImeActionLabel("搜索", EditorInfo.IME_ACTION_SEARCH);
mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
MToastUtil.show("搜索");
}
return false;
}
});
make sure:
inputType = "text";
imeOptions = "actionSend";
After some little more searching - this was the solution for me (works also in a fragment):
Just remove the imeAction

RadioGroup setOnCheckedChangeListener not work with RadioButton setOnTouchListener

I have a RadioGroup with RadioButtons inside. My problem was when i check radiobutton listener will not work. When I delete radiobutton touchlistener event it will works.
final RadioGroup RGroup = (RadioGroup) view.findViewById(R.id.RGroup);
RGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton checkedRadioButton = (RadioButton) group.findViewById(checkedId);
boolean isChecked = checkedRadioButton.isChecked();
if (isChecked)
{
checkedRadioButton.setButtonDrawable(R.drawable.ic_check_black_24dp);
}
}
});
This code works when i remove below code
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int colorFrom = v.getResources().getColor(android.R.color.transparent);
int colorTo = v.getResources().getColor(R.color.colorAnswer);
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
colorAnimation.setDuration(1000); // milliseconds
colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animator) {
button.setBackgroundColor((int) animator.getAnimatedValue());
}
});
colorAnimation.start();
return true;
}
});
Layout of radiobuttons
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="#id/slide_question"
android:layout_marginTop="#dimen/pad_7dp"
android:id="#+id/RGroup">
<android.support.v7.widget.AppCompatRadioButton
android:id="#+id/radioButton1"
android:layout_width="match_parent"
android:layout_marginBottom="#dimen/pad_5dp"
android:padding="#dimen/pad_10dp"
android:layout_height="wrap_content"
android:textSize="14sp"
app:fontFamily="#font/ubuntu"
android:textColor="#color/colorBlackText"
app:buttonTint="#color/colorPrimary"
android:text="RadioButton" />
<android.support.v7.widget.AppCompatRadioButton
android:id="#+id/radioButton2"
android:layout_width="match_parent"
android:layout_marginBottom="#dimen/pad_5dp"
android:padding="#dimen/pad_7dp"
android:layout_height="wrap_content"
android:textSize="14sp"
app:fontFamily="#font/ubuntu"
android:textColor="#color/colorBlackText"
app:buttonTint="#color/colorPrimary"
android:text="RadioButton" />
</RadioGroup>
Maybe listeners conflicts
Yes It is because the listener conflictions. When you are applying touchListener at that time it's stopping the checkChangeListener of the group to get the event.
Still try out return false from the onTouch method check if it's working or not.
Post the behaviour after trying.

Keyboard enter not working on some phones

So I have a problem on my Oneplus 3t that the enter key creates a new line and doesn't actually do action, but on Samsung phones it works fine. This is the EditText
<EditText
android:id="#+id/section"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:layout_toEndOf="#+id/nav_button"
android:layout_toRightOf="#+id/nav_button"
android:background="#android:color/transparent"
android:clickable="false"
android:ems="10"
android:focusable="false"
android:gravity="left"
android:inputType="textMultiLine|textNoSuggestions"
android:maxHeight="140dp"
android:scrollbars="vertical"
android:text="#string/section"
android:textColor="#color/textColor"
android:textSize="20sp" />
And this is how I handle the action
mSearch.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
switch (i) {
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
unReadySeach(section, mSearch, search, mSearch.getText().toString(), mNavButton, backArrow);
gridView.setAdapter(new QuoteImageAdapater(getApplicationContext(), reader.getSearchData(mSearch.getText().toString()), section));
searched = true;
return true;
}
}
return false;
}
});
Also in the virtual device on a pixel 2, when I press on the enter key on the keyboard itself it creates a new line but when I press on enter key on my own keyboard of my pc it does work.
Edit: I fixed it by changing the EditText to this
<EditText
android:id="#+id/searchText"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/section"
android:layout_alignBottom="#+id/section"
android:layout_alignStart="#+id/section"
android:background="#drawable/underline"
android:ems="10"
android:hint="Search"
android:imeOptions="actionSearch"
android:inputType="textNoSuggestions|text"
android:maxHeight="140dp"
android:textColor="#color/textColor"
android:textColorHint="#color/textHint"
android:textCursorDrawable="#null"
android:textSize="20sp"
android:visibility="invisible" />
Try this one because solution you have used, it is not guaranteed for soft keys.
First Solution:
<EditText
android:id="#+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/search_hint"
android:inputType="text"
android:imeOptions="actionSend" />
EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendMessage();
handled = true;
}
return handled;
}
});
Second Solution
mSearch.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER))
{
// Perform action on Enter key press
unReadySeach(section, mSearch, search, mSearch.getText().toString(), mNavButton, backArrow);
gridView.setAdapter(new QuoteImageAdapater(getApplicationContext(), reader.getSearchData(mSearch.getText().toString()), section));
searched = true;
return true;
}
return false;
}
});

How to stop Edit Text cursor moving to next row using Recyclerview

i have implemented a recycler view with edit text for input quantity, after entering the quantity in edit text and calling a method my cursor is moving to next recyler edit text, pulling keyboard again.
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:textSize="14dp"
android:textColor="#333f50"
android:id="#+id/edt_Quantity"
android:text="1"
android:cursorVisible="false"
android:maxLength="2"
android:background="#android:color/transparent"
android:imeOptions="actionDone"
android:inputType="number"
android:padding="5dp" />
we have tried disabling cursor by default in xml and enabling it with following code
holder.edtQuantity.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
holder.edtQuantity.setCursorVisible(true);
}
});
i am expecting the cursor to get invisible(preventing from moving to next recycler) once i call a method associated with it.
Any help will be appreciated.
when you press done button in keyboard is clearFocus of edittext.
holder.edtQuantity.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
holder.edtQuantity.clearFocus();
}
return true;
}
});

requesting focus of another EditText behaves in abnormal ways

I have four EditText's to read a PIN. Now that I have coded the Java side of the EditText it behaves in an abnormal way. After entering one digit in the first EditText it should request focus of the second EditText, however it goes to the third EditText.
And even more it is defined as numberPassword and still the digits are visible.
Here is the source code to the layout file :
<RelativeLayout
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="#+id/mpin_1"
android:layout_marginStart="20dp"
android:layout_centerVertical="true"
android:maxLength="1"
android:gravity="center"
android:layout_width="50dp"
android:inputType="numberPassword"
android:layout_height="50dp" />
<EditText
android:id="#+id/mpin_2"
android:layout_marginStart="20dp"
android:layout_toEndOf="#+id/mpin_1"
android:layout_centerVertical="true"
android:gravity="center"
android:maxLength="1"
android:layout_width="50dp"
android:inputType="numberPassword"
android:layout_height="50dp" />
<EditText
android:id="#+id/mpin_3"
android:layout_marginStart="20dp"
android:layout_toEndOf="#+id/mpin_2"
android:layout_centerVertical="true"
android:gravity="center"
android:layout_width="50dp"
android:maxLength="1"
android:inputType="numberPassword"
android:layout_height="50dp" />
<EditText
android:id="#+id/mpin_4"
android:layout_marginStart="20dp"
android:layout_toEndOf="#+id/mpin_3"
android:gravity="center"
android:layout_centerVertical="true"
android:layout_width="50dp"
android:maxLength="1"
android:inputType="numberPassword"
android:layout_height="50dp" />
</RelativeLayout>
The source code to the java file:
public class SpinActivity extends AppCompatActivity
{
private StringBuilder s_pin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spin);
s_pin = new StringBuilder();
s_pin.setLength(0);
Button sPinButton = (Button)findViewById(R.id.set_s_pin_button);
final EditText e1 = (EditText)findViewById(R.id.mpin_1);
final EditText e2 = (EditText)findViewById(R.id.mpin_2);
final EditText e3 = (EditText)findViewById(R.id.mpin_3);
final EditText e4 = (EditText)findViewById(R.id.mpin_4);
e1.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(e1.getText().length()==1)
s_pin.append(e1.getText().toString());
//e1.setEnabled(false);
e2.requestFocus();
return false;
}
});
e2.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(e2.getText().length()==1)
s_pin.append(e2.getText().toString());
//e2.setEnabled(false);
e3.requestFocus();
return false;
}
});
e3.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(e3.getText().length()==1)
s_pin.append(e3.getText().toString());
//e3.setEnabled(false);
e4.requestFocus();
return false;
}
});
e4.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(e4.getText().length()==1)
s_pin.append(e4.getText().toString());
// e4.setEnabled(false);
return false;
}
});
sPinButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),s_pin.toString(),
Toast.LENGTH_SHORT).show();
}
});
}
}
How to fix this?
Use below code:
android:nextFocusDown="#+id/edittext"
edittext is the ID of the EditText to be focused.

Categories