java - Customize text inside Edittext for chatting -


i want customize text inside edittext of chatting app. want bold username , normal font message. example;

usernamejay: hi jay. how you?

also make 1 space reply after message of usernamejay. want change font color of username. if possible put message balloon every message.

example:

usernamejay: hi jay. how you?

usernameclark: i'm fine. can call meeting?

can me how. code java

import java.io.unsupportedencodingexception; import com.example.healthhelpv2.*;  import android.app.activity; import android.app.alertdialog; import android.app.dialog; import android.app.notificationmanager; import android.content.broadcastreceiver; import android.content.componentname; import android.content.context; import android.content.dialoginterface; import android.content.intent; import android.content.intentfilter; import android.content.serviceconnection; import android.database.cursor; import android.os.bundle; import android.os.handler; import android.os.ibinder; import android.view.keyevent; import android.view.view; import android.view.view.onclicklistener; import android.view.view.onkeylistener; import android.widget.button; import android.widget.edittext; import android.widget.toast; import at.vcity.androidim.interfaces.iappmanager; import at.vcity.androidim.services.imservice; import at.vcity.androidim.tools.friendcontroller; import at.vcity.androidim.tools.localstoragehandler; import at.vcity.androidim.types.friendinfo; import at.vcity.androidim.types.messageinfo;   public class messaging extends activity {      private static final int message_cannot_be_sent = 0;     public string username;     private edittext messagetext;     private edittext messagehistorytext;     private button sendmessagebutton;     private iappmanager imservice;     private friendinfo friend = new friendinfo();     private localstoragehandler localstoragehandler;      private cursor dbcursor;      private serviceconnection mconnection = new serviceconnection() {            public void onserviceconnected(componentname classname, ibinder service) {                       imservice = ((imservice.imbinder)service).getservice();         }         public void onservicedisconnected(componentname classname) {             imservice = null;             toast.maketext(messaging.this, r.string.local_service_stopped,                     toast.length_short).show();         }     };      @override     protected void oncreate(bundle savedinstancestate)      {         // todo auto-generated method stub         super.oncreate(savedinstancestate);              setcontentview(r.layout.messaging_screen); //messaging_screen);          messagehistorytext = (edittext) findviewbyid(r.id.messagehistory);          messagetext = (edittext) findviewbyid(r.id.message);          messagetext.requestfocus();                   sendmessagebutton = (button) findviewbyid(r.id.sendmessagebutton);          bundle extras = this.getintent().getextras();           friend.username = extras.getstring(friendinfo.username);         friend.ip = extras.getstring(friendinfo.ip);         friend.port = extras.getstring(friendinfo.port);         string msg = extras.getstring(messageinfo.messagetext);            settitle("messaging " + friend.username);       //  edittext friendusername = (edittext) findviewbyid(r.id.friendusername);     //  friendusername.settext(friend.username);           localstoragehandler = new localstoragehandler(this);         dbcursor = localstoragehandler.get(friend.username, imservice.username );          if (dbcursor.getcount() > 0){         int noofscorer = 0;         dbcursor.movetofirst();             while ((!dbcursor.isafterlast())&&noofscorer<dbcursor.getcount())              {                 noofscorer++;                  this.appendtomessagehistory(dbcursor.getstring(2) , dbcursor.getstring(3));                 dbcursor.movetonext();             }         }         localstoragehandler.close();          if (msg != null)          {             this.appendtomessagehistory(friend.username , msg);             ((notificationmanager)getsystemservice(notification_service)).cancel((friend.username+msg).hashcode());         }          sendmessagebutton.setonclicklistener(new onclicklistener(){             charsequence message;             handler handler = new handler();             public void onclick(view arg0) {                 message = messagetext.gettext();                 if (message.length()>0)                  {                            appendtomessagehistory(imservice.getusername(), message.tostring());                      localstoragehandler.insert(imservice.getusername(), friend.username, message.tostring());                      messagetext.settext("");                     thread thread = new thread(){                                            public void run() {                             try {                                 if (imservice.sendmessage(imservice.getusername(), friend.username, message.tostring()) == null)                                 {                                      handler.post(new runnable(){                                              public void run() {                                              toast.maketext(getapplicationcontext(),r.string.message_cannot_be_sent, toast.length_long).show();                                               //showdialog(message_cannot_be_sent);                                                                                }                                      });                                 }                             } catch (unsupportedencodingexception e) {                                 toast.maketext(getapplicationcontext(),r.string.message_cannot_be_sent, toast.length_long).show();                                  e.printstacktrace();                             }                         }                                            };                     thread.start();                  }              }});          messagetext.setonkeylistener(new onkeylistener(){             public boolean onkey(view v, int keycode, keyevent event)              {                 if (keycode == 66){                     sendmessagebutton.performclick();                     return true;                 }                 return false;             }           });      }      @override     protected dialog oncreatedialog(int id) {         int message = -1;         switch (id)         {         case message_cannot_be_sent:             message = r.string.message_cannot_be_sent;         break;         }          if (message == -1)         {             return null;         }         else         {             return new alertdialog.builder(messaging.this)                    .setmessage(message)             .setpositivebutton(r.string.ok, new dialoginterface.onclicklistener() {                 public void onclick(dialoginterface dialog, int whichbutton) {                     /* user clicked ok stuff */                 }             })                     .create();         }     }      @override     protected void onpause() {         super.onpause();         unregisterreceiver(messagereceiver);         unbindservice(mconnection);          friendcontroller.setactivefriend(null);      }      @override     protected void onresume()      {                super.onresume();         bindservice(new intent(messaging.this, imservice.class), mconnection , context.bind_auto_create);          intentfilter = new intentfilter();         i.addaction(imservice.take_message);          registerreceiver(messagereceiver, i);          friendcontroller.setactivefriend(friend.username);             }       public class  messagereceiver extends broadcastreceiver {          @override         public void onreceive(context context, intent intent)          {                    bundle = intent.getextras();             string username = extra.getstring(messageinfo.userid);                       string message = extra.getstring(messageinfo.messagetext);              if (username != null && message != null)             {                 if (friend.username.equals(username)) {                     appendtomessagehistory(username, message);                     localstoragehandler.insert(username,imservice.getusername(), message);                  }                 else {                     if (message.length() > 15) {                         message = message.substring(0, 15);                     }                     toast.maketext(messaging.this,  username + " says '"+                                                     message + "'",                                                     toast.length_short).show();                      }             }                    }      };     private messagereceiver messagereceiver = new messagereceiver();      public  void appendtomessagehistory(string username, string message) {         if (username != null && message != null) {             messagehistorytext.append(username + ":\n");                                             messagehistorytext.append(message + "\n");         }     }       @override     protected void ondestroy() {         super.ondestroy();         if (localstoragehandler != null) {             localstoragehandler.close();         }         if (dbcursor != null) {             dbcursor.close();         }     }   } 

the xml messaging_screen

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:background="#ccbfbf"     android:orientation="vertical"     android:padding="10dip" >  <!--         <textview          android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:layout_marginbottom="10dip"         android:text="friend:"     />      <edittext android:id="@+id/friendusername"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:clickable="true"         android:singleline="true"         android:editable="false" /> -->      <textview   android:layout_width="fill_parent"                 android:layout_height="wrap_content"                 android:layout_marginbottom="10dip"                 android:text="messages:"/>      <edittext   android:id="@+id/messagehistory"                 android:layout_width="fill_parent"                 android:layout_height="fill_parent"                 android:clickable="true"                     android:layout_weight="1"                 android:editable="false"                 android:gravity="top"                 android:scrollbars="vertical"                 android:scrollbarsize="10px"                 />       <linearlayout android:orientation="horizontal"                   android:layout_width="fill_parent"                   android:layout_height="fill_parent"                   android:layout_weight="4">              <edittext                 android:id="@+id/message"                 android:layout_width="fill_parent"                 android:layout_height="fill_parent"                 android:layout_weight="1"                 android:gravity="top"                 android:hint="type message here!" />              <button android:id="@+id/sendmessagebutton"                     android:layout_width="fill_parent"                     android:layout_height="fill_parent"                     android:layout_weight="4"                     android:text="send"/>      </linearlayout>  </linearlayout> 

do way hope works you

edittext edittext = (edittext)findviewbyid(r.id.edittext);         string username = "usernamejay";         string message = "hi jay. how you?";         string finalstr = "<b>"+username+":</b> "+message+"";         edittext.settext(html.fromhtml(finalstr)); 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -