Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show typing indicator in android firebase chat [duplicate]

I need to implement typing indicator in chat application using firebase. I have a logic like

  • Add a textwatcher listener and in text change listener update flag of a field named typing in firebase db
  • Then i think when a value change it will notify the value change listener

    if any one has a code implementation please share.

enter image description here

like image 562
Jins Lukose Avatar asked Feb 15 '19 04:02

Jins Lukose


2 Answers

Please check code below: `

make structure something like: (it may vary as per your need I am just referring something to you)

enter image description here

I have an object "isTyping" with Two Keys Let's say "first_user" and "second_user". If you want to check for "second_user" is typing or not this should be the code.

  private void checkForOtherUserTypingStatus() {
        mMessageThread.child("isTyping").addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                if (dataSnapshot.getKey().equalsIgnoreCase("second_user") && dataSnapshot.getValue().toString().equals("true")) {
                    updateTypingViewVisibility(true);
                } else if (dataSnapshot.getKey().equalsIgnoreCase("second_user") && dataSnapshot.getValue().toString().equals("false")) {
                    updateTypingViewVisibility(false);
                }
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                if (dataSnapshot.getValue() != null) {
                    if (dataSnapshot.getKey().equalsIgnoreCase("second_user") && dataSnapshot.getValue().toString().equals("true")) {
                        updateTypingViewVisibility(true);
                    } else if (dataSnapshot.getKey().equalsIgnoreCase("second_user") && dataSnapshot.getValue().toString().equals("false")) {
                        updateTypingViewVisibility(false);
                    }
                }
            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

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

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

To update value of "isTyping" > "first_user" (assume that you are typing)

 mBinder.etChatMessage.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {


                Map<String, Object> params = new HashMap<>();
                params.put("first_user", charSequence.length() > 0);
                mMessageThread.child("isTyping").updateChildren(params);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
like image 177
Deep Patel Avatar answered Sep 28 '22 02:09

Deep Patel


Yes . you got it right. in user node maintain one boolean flag like isTyping.

on textwatcherListener change the value to true and yeah onValueChangeListner will also trigger so that you can set Typing on recipients screen by getting that boolean Value

like image 37
Tejas Pandya Avatar answered Sep 28 '22 03:09

Tejas Pandya