[Android] chat demo

1. Main functions:

①Realization of chat interface

② Status bar immersion

③ The soft keyboard pops up automatically

④ ScrollView automatically scrolls after sending a message, and makes EditText get the focus again


2. On the previous interface, see if it meets your needs. If it matches, continue to look down



3. The main interface is implemented in MainActivity

//onCreate

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//Immersive status bar implementation
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);//Automatically recalculate the layout after the soft keyboard pops up
scrollView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {//Registration event for scrollview registration layout change
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                if (mHandler != null) {
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                                                         scrollView.fullScroll(FOCUS_DOWN);//scrollView automatically slide to the bottom
                                                         msg.setFocusable(true);//The next thing is to return focus to EditText
                            msg.setFocusableInTouchMode(true);
                            msg.requestFocus();
                            msg.findFocus();
                        }
                    });
                }
            }
        });

4. Realization of immersive status bar

The root layout is focused on fitsSysytemWindows="true"

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/main_color"
    android:fitsSystemWindows="true">   


5. Classification of chat content

private class ChatMsgAdapter extends RecyclerView.Adapter<ChatMsgAdapter.ViewHolder> {

        private ArrayList<MsgChatBody> list;

        public ChatMsgAdapter(ArrayList<MsgChatBody> list) {
            this.list = list;
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            public View view;
            public TextView chatMsgLeft;
            public TextView chatMsgRight;

            public ViewHolder(View itemView) {
                super(itemView);
                view = itemView;
                                 chatMsgLeft = view.findViewById(R.id.chat_msg_left);//The layout on the left
                                 chatMsgRight = view.findViewById(R.id.chat_msg_right);//right layout
            }
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_msg_content,
                    parent, false);
            ViewHolder viewHolder = new ViewHolder(view);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            MsgChatBody body = list.get(position);
                         if (body.type == CHAT_MSG_TYPE_LEFT) {//Judging by the type field, which layout is displayed
                holder.chatMsgRight.setVisibility(View.GONE);
                holder.chatMsgLeft.setVisibility(VISIBLE);
                holder.chatMsgLeft.setText(body.msg);
            } else if (body.type == CHAT_MSG_TYPE_RIGHT) {
                holder.chatMsgLeft.setVisibility(View.GONE);
                holder.chatMsgRight.setVisibility(VISIBLE);
                holder.chatMsgRight.setText(body.msg);
            }
        }

        @Override
        public int getItemCount() {
            return list.size();
        }
    }

6.github address

A simple chat app interface implementation

Intelligent Recommendation

Android Bluetooth Communication / Bluetooth chat implementation (1) _ including Demo download

Decree of the basic operation of Android Bluetooth Communication / Bluetooth chat      This series of articles mainly introduce Android devices based on Bluetooth communication, an...

Android Bluetooth Communication / Bluetooth chat implementation (2) _ including Demo download

Android Bluetooth Communication / Bluetooth Chat Chat Communication This series of articles main introductionAndroidThe device is real-time chat based on Bluetooth communication, and the article serie...

swoole chat room demo

Achieve results socket chat room github demo What is SWOOLE Official website says: Swoole: PHP asynchronous network communication engine for the production environment The PHP developers to write high...

Websocket chat rooms by the Demo

Websocket very hot nowadays, it isA new TCP-based network protocol. It implements the browser and server full-duplex (full-duplex) communication - allows the server actively sends information to the c...

More Recommendation

WebSocket chat room Demo

1. WebSocket definition The WebSocket protocol is a new network protocol based on TCP. It implements full-duplex communication between the browser and the server-allowing the server to actively send i...

netty5.0 group chat demo

Server startup and configuration Server processor Client startup and configuration Client processor  ...

Qt tcp chat demo

Client mainwindow.h main.cpp mainwindow.cpp Server mainwindow.h main.cpp mainwindow.cpp interface...

Qt UDP chat Demo

Client mainwindow.h main.cpp mainwindow.cpp Server widget.h main.cpp widget.cpp interface...

A chat server Demo

First contact network programming. A Netty-based WebSocket chat room Demo is done. (It is really just a demo, it may not be possible at all). Record. Service class GroupChatService.java enumerate MSGT...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top