0

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);
    }
2
  • try setting setEnabled(true) and setClickable(true) Commented Nov 11, 2019 at 10:53
  • Could you share the entire NewExpenseActivity class. It's hard to tell whats going on from what you have provided. Commented Nov 11, 2019 at 12:04

1 Answer 1

1

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.

onEditorAction

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

enter image description here

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;
            }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.