Write a beautiful chat interface, then you must have received the message and send the message. First, write the main interface and modify the code in Activity_chat.xml as follows: <?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android = "Http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "Match_ Parent "android:background=" #d8e0e8 "android:orientation=" vertical "> <listviewandroid:id=" @+id/msg_ List_view "android:layout_width=" match_parent "android:layout_height=" 0DP "android:layout_weight=" 1 "android: Divider= "#0000"/> <linearlayoutandroid:layout_width= "match_parent" android:layout_height= "wrap _content "> <edittextandroid:id=" @+id/input_text "android:layout_width=" 0DP "android:layout_height=" Wrap_content "android:layout_weight=" 1 "android:hint=" Type something Here "android:maxlines=" 2 "/> < Buttonandroid:id= "@+id/send" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android: text= "Send"/> </linearlayout> </linearlayout> here in the main interface a ListView is used to display the message content of the chat, and then placeA edittext is used to enter a chat message, and a button is placed to send the message. A Adnroid:divider property is used in the ListView that specifies the color of the ListView split line, where #0000 indicates that the split line is set to a transparent color. Then define the entity class for the message, create a new MSG, and the code looks like this: public class MSG { public static final int type_receiver = 0;public static final int Type_sen D = 1; private string content;private int type; public Msg (String content, int type) {this.content = Content;thi S.type = type;} public String getcontent () {return content;} public int GetType () {return type;} There are only two fields in the }msg class, the content represents the contents, and type represents the type of message. Write the layout of the ListView subkey <?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "/http/ Schemas.android.com/apk/res/android "android:layout_width=" match_parent "android:layout_height=" Match_parent " android:orientation= "vertical" android:padding= "10DP" > <linearlayoutandroid:id= "@+id/left_layout" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "left" Android: background= "@drawable/ease_chatfrom_bg_focused" android:visibility= "Gone"> <textviewandroid:id= "@+id/left_msg" android:layout_width= "wrap_content" android:layout_height= "wrap _content "android:layout_gravity=" center "android:layout_margin=" 10DP "android:textcolor=" #000 "/> </ Linearlayout> <linearlayoutandroid:id= "@+id/right_layout" android:layout_width= "Wrap_content" Android : layout_height= "wrap_content" android:layout_gravity= "right" android:background= "@drawable/ease_chatto_bg_ Focused "android:visibility=" Gone "><textviewandroid:id=" @+id/right_msg "android:layout_width=" wrap_content "Android:layout_height=" wrap_content "android:layout_gravity=" center "android:layout_margin=" 10DP "Android: Textcolor= "#fff"/> </linearlayout> </linearlayout> to write the adapter public for the ListView Class Msgadapter extends Arrayadapter<msg>{ private int resourceid; public Msgadapter (Context context , int Textviewresourceid, list<msg> objects) {Super (context, Textviewresourceid, objects); resourceId = Textviewresourceid;} @Overridepublic View GetView (int position, view Convertview, ViewGroup parent) {MSG msg = GetItem (position); View view; Viewholder viewholder;if (Convertview = = null) {view = Layoutinflater.from (GetContext ()). Inflate (resourceId, NULL); Viewholder = new Viewholder (); viewholder.leftlayout = (linearlayout) View.findviewbyid (r.id.left_layout); Viewholder.rightlayout = (linearlayout) View.findviewbyid (r.id.right_layout); viewholder.leftmsg = (TextView) View.findviewbyid (r.id.left_msg); viewholder.rightmsg = (TextView) View.findviewbyid (r.id.right_msg); View.settag (Viewholder);} else {view = Convertview;viewholder = (viewholder) View.gettag ();} if (msg.gettype () = = Msg.type_receiver) {//Receive messages to the left of the message viewHolder.rightLayout.setVisibility (View.gone); ViewHolder.leftLayout.setVisibility (view.visible); ViewHolder.leftMsg.setText (Msg.getcontent ());} else if (msg.gettype () = = Msg.type_send) {//Send message to the right of message viewHolder.leftLayout.setVisibility (View.gone); ViewHolder.rightLayout.setVisibility (view.visible); vIewHolder.rightMsg.setText (Msg.getcontent ());} return view;} class Viewholder{linearlayout leftlayout; LinearLayout rightlayout; TextView leftmsg; TextView rightmsg; } write Chatactivitypublic class chatactivity extends Activity{private msgadapter Msgadapter;p rivate EditText inputtext;p rivate Button send;p rivate ListView mlistview; arraylist<msg> msglist = new arraylist<msg> (); @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activity_chat); Initmsgs () Msgadapter = new Msgadapter (Chatactivity.this, R.layout.chat_ List_item, msglist); inputtext = (EditText) Findviewbyid (r.id.input_text); send = (Button) Findviewbyid (r.id.send); Mlistview = (ListView) Findviewbyid (R.id.msg_list_view); Mlistview.setadapter (Msgadapter); Send.setonclicklistener ( New Onclicklistener () { @Overridepublic void OnClick (View v) {String content = Inputtext.gettext (). ToString (); if (! "". Equals (content)) {msg msg = new MSG (content, msg.type_send); Msglist.add (MSG); msgadapter.notifydatasetchanged ();// When a message is refreshed, the ListView list displays Mlistview.setselection (Msglist.size ());//The ListView is positioned in the last row Inputtext.settext ("");//Input box contents empty} }});} private void Initmsgs () {MSG MSG1 = new msg ("Hello", msg.type_receiver); Msglist.add (MSG1); msg msg2 = new MSG (" Hello who's What ", Msg.type_send); Msglist.add (MSG2); msg msg3 = new MSG (" Your Friend ", Msg.type_receiver); Msglist.add (MSG3); } }
Customize Android's Beautiful chat interface