This article illustrates the method of Android programming to realize the bubble chat interface. Share to everyone for your reference, specific as follows:
Yesterday I wrote an interface to implement the Android bubble chat interface. The results of the operation are as follows, click the Send button, the screen will display text content.
I am also a source on the web based on the changes, the entire bubble interface to achieve the main points:
(1) The main interface is actually a list View
(2) The text display interface actually uses the android:background= "@drawable/incoming" this thing. The format of the background picture is xxx.9.png, specifically for scaling, otherwise the display effect is very poor.
(3) A custom adapter, of course, is inherited Android.widget.BaseAdapter, rewrite the GetView method.
The whole project is distributed as follows:
The main activity:chatactivity are as follows:
Package com.tencent;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.ListView;
Import java.util.ArrayList;
Import Java.util.Calendar;
public class Chatactivity extends activity {private static final String TAG = ChatActivity.class.getSimpleName ();;
Private ListView Talkview;
Private Button Messagebutton;
Private EditText MessageText;
Private Chatmsgviewadapter Myadapter;
Private arraylist<chatmsgentity> list = new arraylist<chatmsgentity> ();
public void OnCreate (Bundle savedinstancestate) {log.v (TAG, "OnCreate >>>>>>");
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Talkview = (ListView) Findviewbyid (r.id.list);
Messagebutton = (Button) Findviewbyid (R.id.messagebutton); MessageText = (EditText) Findviewbyid (R.id.messagetext);
Onclicklistener Messagebuttonlistener = new Onclicklistener () {@Override public void OnClick (View arg0) {
TODO auto-generated Method Stub log.v (TAG, "onclick >>>>>>>>");
String name = GetName ();
String date = GetDate ();
String Msgtext = GetText ();
int RId = R.layout.list_say_he_item;
Chatmsgentity newmessage = new Chatmsgentity (name, date, Msgtext, RId);
List.add (Newmessage);
List.add (D0);
Talkview.setadapter (New Chatmsgviewadapter (Chatactivity.this, list));
Messagetext.settext ("");
Myadapter.notifydatasetchanged ();
}
};
Messagebutton.setonclicklistener (Messagebuttonlistener); }//Shuold be redefine to future private String GetName () {return getresources (). getString (R.string.mydis
Playname); }//Shuold be redefine in the future private String geTdate () {Calendar c = calendar.getinstance (); String date = string.valueof (C.get (calendar.year)) + "-" + string.valueof (C.get (calendar.month)) + "-" + C.get (c.
Get (Calendar.day_of_month));
return date;
}//Shuold be redefine to future private String GetText () {return Messagetext.gettext (). toString ();
public void OnDestroy () {LOG.V (TAG, "ondestroy>>>>>>");
list = null;
Super.ondestroy ();
}
}
Display the definition of the body of a message
package com.tencent;
public class Chatmsgentity {private static final String TAG = ChatMsgEntity.class.getSimpleName ();
private String name;
Private String date;
private String text;
private int layoutid;
Public String GetName () {return name;
public void SetName (String name) {this.name = name;
Public String GetDate () {return date;
public void setdate (String date) {this.date = date;
Public String GetText () {return text;
public void SetText (String text) {this.text = text;
public int Getlayoutid () {return layoutid;
The public void Setlayoutid (int layoutid) {this.layoutid = LayoutID; Public chatmsgentity () {} public chatmsgentity (string name, string date, string text, int layoutid) {Supe
R ();
THIS.name = name;
This.date = date;
This.text = text;
This.layoutid = LayoutID; }
}
The
Chatmsgviewadapter is defined as follows:
Package com.tencent;
Import Android.content.Context;
Import Android.database.DataSetObserver;
Import Android.util.Log;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.LinearLayout;
Import Android.widget.TextView;
Import java.util.ArrayList; public class Chatmsgviewadapter extends Baseadapter {private static final String TAG = ChatMsgViewAdapter.class.getSim
Plename ();
Private Arraylist<chatmsgentity> Coll;
Private context CTX;
Public Chatmsgviewadapter (Context context, arraylist<chatmsgentity> coll) {ctx = context;
This.coll = coll;
public Boolean areallitemsenabled () {return false;
public boolean isenabled (int arg0) {return false;
public int GetCount () {return coll.size ();
Public Object getitem (int position) {return coll.get (position);
public long getitemid (int position) { return position;
public int Getitemviewtype (int position) {return position; Public View GetView (int position, View Convertview, ViewGroup parent) {LOG.V (TAG, "getview>>>>>&
Gt;> ");
chatmsgentity entity = coll.get (position);
int itemlayout = Entity.getlayoutid ();
LinearLayout layout = new LinearLayout (CTX);
Layoutinflater VI = (layoutinflater) ctx.getsystemservice (Context.layout_inflater_service);
Vi.inflate (itemlayout, layout, true);
TextView tvname = (TextView) Layout.findviewbyid (r.id.messagedetail_row_name);
Tvname.settext (Entity.getname ());
TextView tvdate = (TextView) Layout.findviewbyid (r.id.messagedetail_row_date);
Tvdate.settext (Entity.getdate ());
TextView Tvtext = (TextView) Layout.findviewbyid (R.id.messagedetail_row_text);
Tvtext.settext (Entity.gettext ());
return layout;
public int Getviewtypecount () {return coll.size (); public boolean hasstableids () {return false;
public Boolean IsEmpty () {return false; The public void Registerdatasetobserver (Datasetobserver observer) {} is public void Unregisterdatasetobserver (DataSet
Observer Observer) {}}
Layout file See me more painful, this layout file is not good, hehe
Full instance code code click here to download the site.
I hope this article will help you with the Android program.