Reputation: 749
There's an EditText in my activity and every time the user presses enter button on the keyboard, using OnEditorActionListener
another EditText will be added to the LinearLayout.
The problem is after adding those views, the Button onClick doesn't work. Why is this happening and how to fix it?
button onClick
:
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(NewExpenseActivity.this, "Saved", Toast.LENGTH_SHORT).show();
}
});
.
private TextView.OnEditorActionListener editorActionListener = new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
createNewEditText();
}
return false;
}
};
.
public void createNewEditText() {
textInputLayout = new TextInputLayout(this);
textInputLayout.setPadding(padding_in_px_16, padding_in_px_8, padding_in_px_16, padding_in_px_8);
editText = new EditText(NewExpenseActivity.this);
editText.setId(id);
editText.setHint("Enter Name");
editText.setInputType(InputType.TYPE_CLASS_TEXT);
editText.setOnEditorActionListener(editorActionListener);
editText.setImeOptions(EditorInfo.IME_ACTION_NEXT);
textInputLayout.addView(editText);
ITEM_MAP.put("Key" + idNum, id);
idNum++;
linearEtList.addView(textInputLayout);
}
Upvotes: 0
Views: 96
Reputation:
Try using :-
private TextView.OnEditorActionListener editorActionListener = new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if (event != null) {
createNewEditText();
}
return false;
}
}
Because of :-
actionId int: Identifier of the action. This will be either the identifier you supplied, or EditorInfo#IME_NULL if being called due to the enter key being pressed.
event If triggered by an enter key, this is the event; otherwise, this is null.
And the setImeOptions(EditorInfo.IME_ACTION_NEXT) adds/sets the softkeyboard to have a NEXT (--->|
) button. Only if that soft button is used will the actionId == IME_ACTION_NEXT.
like
If you want both then you can do
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
addEditText();
}
return false;
}
Upvotes: 1