Search code examples
javaandroidandroid-studiochatroom

I want to clear the text in the Plain TextBox when I press the button but cant find anything to do so


public class Chat_Room extends AppCompatActivity {

    private Button btn_send_msg;
    private EditText input_msg;
    private TextView chat_conversation;

    private String user_name,room_name;
    private DatabaseReference root;
    private String temp_key;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.chat_room);

    btn_send_msg = (Button) findViewById(R.id.button13);
    input_msg = (EditText) findViewById(R.id.editText3);
    chat_conversation = (TextView) findViewById(R.id.textView4);

    user_name = getIntent().getExtras().get("user_name").toString();
    room_name = getIntent().getExtras().get("room_name").toString();
    setTitle("Room - " + room_name);

    root = FirebaseDatabase.getInstance().getReference().child(room_name);

    btn_send_msg.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {

                                            Map<String, Object> map = new HashMap<String, Object>();
                                            temp_key = root.push().getKey();
                                            root.updateChildren(map);

                                            DatabaseReference message_root = root.child(temp_key);
                                            Map<String, Object> map2 = new HashMap<String, Object>();
                                            map2.put("name", user_name);
                                            map2.put("msg", input_msg.getText().toString());

                                            message_root.updateChildren(map2);
                                        }
                                    });


    root.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            append_chat_conversation(dataSnapshot);
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            append_chat_conversation(dataSnapshot);
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}
private String chat_msg,chat_user_name;
private void append_chat_conversation(DataSnapshot dataSnapshot) {

    Iterator i = dataSnapshot.getChildren().iterator();

    while (i.hasNext()){

        chat_msg = (String) ((DataSnapshot)i.next()).getValue();
        chat_user_name = (String) ((DataSnapshot)i.next()).getValue();

        chat_conversation.append(chat_user_name + " : " + chat_msg + "\n");
    }
}

Basically I want to be able to send a message using the button which currently happens but I want the text that has been typed and sent to disappear when the button is clicked.

This is a chat room so this is imperative to stop the user getting annoyed.

Thanks!


Solution

  • You will need to set the text of the textview to an empty string:

    textview.setText("");