0

I am making an app that sends data over bluetooth to a HC-06. When I type something in the EditText and press send, the EditText loses focus. I found out this happens because I call a backgound thread with write().

How can I keep focus?

messageEdTxt = findViewById(R.id.message_edtxt);
    messageEdTxt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEND) {
                String messageEdtxt = messageEdTxt.getText().toString();
                messageEdTxt.setText("");
                write(messageEdtxt);
                return true;
            } else {
                return false;
            }
        }
    });

    sendBtn = findViewById(R.id.send_btn);
    sendBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String messageEdtxt = messageEdTxt.getText().toString();
            write(messageEdtxt);
            messageEdTxt.setText("");
        }
    });

The mConnectedThread is a custom thread to send messages.

private void write(String message) {
    if (mSocket.isConnected()) {
        if (mConnectedThread != null) {
            mConnectedThread.write(message.getBytes(StandardCharsets.UTF_8));
        } else {
            finish();
        }
    } else {
        finish();
    }
}
Robbe
  • 53
  • 7

1 Answers1

0

Did you try

messageEdTxt.requestFocus();

on clicking sendBtn