Search code examples
androidcheckboxdynamicandroid-edittextandroid-softkeyboard

Using onEditorActionListener doesn't work for the second time in android


I am new to android development, and what I am trying to achieve is this:

when the user clicks the DONE button via the on-screen keyboard (soft input method), the existing default editText should turn into a checkBox and also again create an editText so that the user can enter some data again.

These I should be able to do this with creating a button and pressing it but I don't want a button, I want the on-screen keyboard interactions such as DONE button.

First, I tried it with onKeyListener but it doesn't work on the soft keyboard(mobile phone's) instead it worked on the hardware keyboard such as the laptop keyboard.

And then I am also able to do the above-mentioned function via the On-Screen Keyboard using the onEditerActionListener(), but only once I can do this and after that, the "DONE" button disappears from the on-Screen keyboard, the ENTER button replaces the DONE button.

Edit I make Done from kind of always appears whenever I focus on the editText using editText.setSingleLine(), but this time when I click the editorActionListener doesn't work?

I don't know why done button doesn't work the way it worked when for the first time I tapped

XML:

    <EditText
        android:id="@+id/defaultEditText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="32dp"
        android:inputType="text"
        android:textSize="20sp"
        android:imeOptions = "actionDone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

Android code:

    TextView.OnEditorActionListener doneButtonListener = ( new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

        if (actionId == EditorInfo.IME_ACTION_DONE) {

            //CREATING A CHECKBOX
            CheckBox toDoCheckBox = new CheckBox(MainActivity.this);
            toDoCheckBox.setText(defaultEditText.getText().toString());
            toDoCheckBox.setTextSize(20);
            toDoCheckBox.setId(View.generateViewId());
            previousViewId = toDoCheckBox.getId();

            ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) defaultEditText.getLayoutParams();

            ConstraintLayout.LayoutParams newParams = new ConstraintLayout.LayoutParams(
                    ConstraintLayout.LayoutParams.WRAP_CONTENT,
                    ConstraintLayout.LayoutParams.WRAP_CONTENT
            );

            newParams.width = params.width;
            newParams.height = params.height;

            //Constraints
            newParams.startToStart = params.startToStart;
            newParams.endToEnd = params.endToEnd;
            newParams.topToTop = params.topToTop;
            newParams.topToBottom = params.topToBottom;

            //Margins
            newParams.leftMargin = params.leftMargin;
            newParams.topMargin = params.topMargin;
            newParams.rightMargin = params.rightMargin;

            constraintLayout.addView(toDoCheckBox, -1, newParams);
            defaultEditText.setVisibility(View.INVISIBLE);


            // CREATING A EDITTEXT
            EditText newEditText = new EditText(MainActivity.this);
            newEditText.setWidth(defaultEditText.getWidth());
            newEditText.setHeight(defaultEditText.getHeight());
            newEditText.setId(View.generateViewId());
            newEditText.setSingleLine();

            ConstraintLayout.LayoutParams editTextParams = new ConstraintLayout.LayoutParams(
                    ConstraintLayout.LayoutParams.WRAP_CONTENT,
                    ConstraintLayout.LayoutParams.WRAP_CONTENT
            );

            if (isFirst) {
                editTextParams.topToBottom = previousViewId;
                editTextParams.startToStart = previousViewId;
                isFirst = false;

            } else {
                editTextParams.topToBottom = previousViewId;
                editTextParams.startToStart = previousViewId;

            }

            defaultEditText = newEditText.findViewById(newEditText.getId());
            newEditText.setLayoutParams(editTextParams);
            constraintLayout.addView(newEditText);

            return true;
        }
        return false;
    }
});

    defaultEditText.setOnEditorActionListener(doneButtonListener);
}

Output:

enter image description here

enter image description here

I am reposting this question as I didn't get answer for my question


Solution

  • You are creating the new edit text at run, you need to initialize the editor action lister for new edit text also. Here's what updated code should look like

    defaultEditText = findViewById(R.id.defaultEditText);
    defaultEditText.setOnEditorActionListener(this);
    

    Editor Action

     @Override
        public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
            //CREATING A CHECKBOX
            CheckBox toDoCheckBox = new CheckBox(TestingAcitvity.this);
            toDoCheckBox.setText(defaultEditText.getText().toString());
            toDoCheckBox.setTextSize(20);
            toDoCheckBox.setId(View.generateViewId());
            previousViewId = toDoCheckBox.getId();
    
            ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) defaultEditText.getLayoutParams();
    
            ConstraintLayout.LayoutParams newParams = new ConstraintLayout.LayoutParams(
                    ConstraintLayout.LayoutParams.WRAP_CONTENT,
                    ConstraintLayout.LayoutParams.WRAP_CONTENT
            );
    
            newParams.width = params.width;
            newParams.height = params.height;
    
            //Constraints
            newParams.startToStart = params.startToStart;
            newParams.endToEnd = params.endToEnd;
            newParams.topToTop = params.topToTop;
            newParams.topToBottom = params.topToBottom;
    
            //Margins
            newParams.leftMargin = params.leftMargin;
            newParams.topMargin = params.topMargin;
            newParams.rightMargin = params.rightMargin;
    
            constraintLayout.addView(toDoCheckBox, -1, newParams);
            defaultEditText.setVisibility(View.INVISIBLE);
    
    
            // CREATING A EDITTEXT
            EditText newEditText = new EditText(TestingAcitvity.this);
            newEditText.setWidth(defaultEditText.getWidth());
            newEditText.setHeight(defaultEditText.getHeight());
            newEditText.setId(View.generateViewId());
            newEditText.setSingleLine();
    
            ConstraintLayout.LayoutParams editTextParams = new ConstraintLayout.LayoutParams(
                    ConstraintLayout.LayoutParams.WRAP_CONTENT,
                    ConstraintLayout.LayoutParams.WRAP_CONTENT
            );
    
            if (isFirst) {
                editTextParams.topToBottom = previousViewId;
                editTextParams.startToStart = previousViewId;
                isFirst = false;
            } else {
                editTextParams.topToBottom = previousViewId;
                editTextParams.startToStart = previousViewId;
            }
    
            newEditText.requestFocus(); // request focus to focus on edit text
            defaultEditText = newEditText.findViewById(newEditText.getId());
            newEditText.setLayoutParams(editTextParams);
            defaultEditText.setOnEditorActionListener(this); // set editor action listener
            constraintLayout.addView(newEditText);
    
            return true;
        }