android - How to use two drawable background images in custom listview for a messaging app -


i'm developing kind of messaging app android. when messages loaded custom list view database, displayed ok. want them displayed native messaging app of android. have 2 images sender , receiver using 9patch. want display image http://s21.postimg.org/bj6idzdaf/example.png. i'm having http://s23.postimg.org/805pfdxqz/example1.png. here code of row:

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/chatlinearlayout"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="@drawable/me"     android:orientation="vertical" >  <!-- user , createdat -->  <linearlayout     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:orientation="horizontal"     android:paddingbottom="0dp" >      <textview         android:id="@+id/textuser"         android:layout_width="0dip"         android:layout_height="wrap_content"         android:layout_weight="1"         android:text="user"         android:textsize="15sp" />      <textview         android:id="@+id/textcreatedat"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="less minute"         android:textsize="10sp" /> </linearlayout>  <!-- message -->  <textview     android:id="@+id/texttext"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:paddingtop="0dp"     android:text="i&apos;m text"     android:textsize="12sp" />      </linearlayout> 

here code of layout:

<?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" >  <listview     android:id="@+id/listtimeline"     android:layout_width="fill_parent"     android:layout_height="0dip"     android:layout_weight="1"     android:divider="#ffffff"     android:dividerheight="1dp"     android:listselector="@drawable/list_selector" />  <linearlayout     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:orientation="horizontal" >      <edittext         android:id="@+id/messagetext"         android:layout_width="0dip"         android:layout_height="wrap_content"         android:layout_weight="1"         android:ems="10"         android:hint="@string/sendtextboxhint" />      <button         android:id="@+id/buttonsend"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/buttonsend" /> </linearlayout>  </linearlayout> 

here code of getting , displaying messages in list view

class getanddisplaymessages extends thread {       @override     public void run() {         while (!stopcheckingformessages) {             try {                 theapp.appdata.openreadabledatabase();                 cursor = theapp.appdata.getmessages(user,                         theapplication.screenname);                 log.d(tag, "cursor containing " + cursor.getcount()                         + " rows");                 if (cursor == null) {                     return;                 }                 startmanagingcursor(cursor);                 chatactivity.this.runonuithread(new runnable() {                     @suppresswarnings("deprecation")                     public void run() {                         string[] = { appdata.t_time,                                 appdata.sender_screen_name,                                 appdata.messages };                         int[] = { r.id.textcreatedat, r.id.textuser,                                 r.id.texttext };                         adapter = new simplecursoradapter(                                 chatactivity.this, r.layout.chat_row,                                 cursor, from, to);                         adapter.setviewbinder(view_binder);                          listtimeline.setadapter(adapter);                         listtimeline.setselection(adapter.getcount()-1);                     }                 });                  thread.sleep(delay);             } catch (exception e) {                 log.e(tag,                         "error occured while checking updated messages. \nerrormessage:"                                 + e.getmessage());             }         }     }  } 

you need create custom adaptor work:-

and inside getview method can insert different xml layout:-

listitemsender_layout.xml listitemreciever_layout.xml

    public view getview(final int position, view convertview, viewgroup parent) {             if(convertview == null)             {                  layoutinflater vi = (layoutinflater) context                         .getsystemservice(context.layout_inflater_service);                   if(id == sender_id)                 {                         convertview = vi.inflate(r.layout.listitemsender_layout, null);                 }else                 {                         convertview = vi.inflate(r.layout.listitemreciever_layout, null);                 }     } 

and can set different background image in these xml layout. other components similer in both of layouts.

thanks.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -