android - Alternate chat bubble widths -



android - Alternate chat bubble widths -

i developing chat type application. using 2 different 9 patches chat bubble main message , responses. problem facing automatically wrapping widths of bubbles according message length. next main layout:

<relativelayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" > <listview android:id="@+id/list" android:layout_width="wrap_content" android:layout_height="fill_parent" android:stackfrombottom="true" android:transcriptmode="alwaysscroll" android:cachecolorhint="#00000000" android:listselector="@android:color/transparent" android:divider="@null" /> </relativelayout> <linearlayout android:id="@+id/footer" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="bottom" style="@android:style/buttonbar"> <button android:id="@+id/stt_button" android:layout_width="45dp" android:layout_height="50dp" android:background="@drawable/microphone" /> <edittext android:id="@+id/chat_msg" android:inputtype="text" android:layout_width="0dp" android:layout_height="40dp" android:layout_weight="1" /> <button android:id="@+id/send_button" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_gravity="center_vertical" android:text="@string/send_button" /> </linearlayout> </linearlayout>

this list_item layout:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="wrap_content" android:weightsum="1.0" android:layout_weight="1" android:layout_height="wrap_content"> <linearlayout android:id="@+id/linearlayoutleft" android:layout_width="0dp" android:layout_weight="0.8" android:layout_height="wrap_content"> <relativelayout android:id="@+id/relativelayoutleft" android:layout_width="wrap_content" android:layout_height="wrap_content"> <textview android:id="@+id/lefttext" android:layout_width="wrap_content" android:gravity="top" android:layout_height="wrap_content" android:paddingleft="10dip" android:paddingright="10dip" android:paddingtop="5dip" android:layout_alignparentleft="true"> </textview> </relativelayout> </linearlayout> <linearlayout android:id="@+id/linearlayoutright" android:layout_width="0dp" android:layout_weight="0.8" android:layout_height="wrap_content"> <relativelayout android:id="@+id/relativelayoutright" android:layout_width="wrap_content" android:layout_height="wrap_content"> <textview android:id="@+id/righttext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingleft="10dip" android:paddingright="10dip" android:paddingtop="5dip" android:layout_alignparentright="true" android:layout_alignparenttop="true"> </textview> </relativelayout> </linearlayout>

this code within getview method of custom array adapter:

view view = convertview; if(view == null){ view = minflater.inflate(r.layout.list_item, null); } resources res = getcontext().getresources(); drawable bubbleschat = res.getdrawable(r.drawable.bubbles_chat); drawable bubblesresponse = res.getdrawable(r.drawable.bubbles_response); textview left = (textview) view.findviewbyid(r.id.lefttext); textview right = (textview) view.findviewbyid(r.id.righttext); view leftlayout = view.findviewbyid(r.id.relativelayoutleft); view rightlayout = view.findviewbyid(r.id.relativelayoutright); linearlayout linearleft = (linearlayout) view.findviewbyid(r.id.linearlayoutleft); linearlayout linearright = (linearlayout) view.findviewbyid(r.id.linearlayoutright); layoutparams leftparams = (layoutparams) linearleft.getlayoutparams(); layoutparams rightparams = (layoutparams) linearright.getlayoutparams(); string txt = super.getitem(position); if(txt.startswith("s:")) { left.settext(getitem(position)); leftlayout.setbackgrounddrawable(bubbleschat); leftparams.weight = 0.8f; linearleft.setlayoutparams(leftparams); rightparams.weight = 0.2f; linearright.setlayoutparams(rightparams); right.settext(""); rightlayout.setbackgrounddrawable(null); } else { right.settext(getitem(position)); rightlayout.setbackgrounddrawable(bubblesresponse); rightparams.weight = 0.8f; linearright.setlayoutparams(rightparams); leftparams.weight = 0.2f; linearleft.setlayoutparams(leftparams); left.settext(""); leftlayout.setbackgrounddrawable(null); } homecoming view;

all getting setup next (note empty spaces in front end of right bubbles:

you can see right hand side bubbles not wrapping width according text size. understand why happening - assigning weight of 0.8 current chat message bubble (may left of right) , 0.2 rest. when current message left bubble works fine draw first 0.8 weight wrap_content. when right bubble current message, left bubble got drawn first , have fixed width of 0.2, hence right 1 gets 0.8 irrespective of wrap_content. how can prepare this? want bubbles according text width , aligned either left or right. can ditch current way altogether if can suggest improve way correctly.

when first posted question, started android programming. problem overly complicated layout definitions , code. simplified layout hierarchy, have achieved wanted , without using layout weight , lot simple code/configuration. here posting updated code snippets.

my main layout now:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="fill_parent"> <listview android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:stackfrombottom="true" android:transcriptmode="alwaysscroll" android:cachecolorhint="#00000000" android:listselector="@android:color/transparent" android:divider="@null"/> <linearlayout android:id="@+id/footer" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="bottom" style="@android:style/buttonbar"> <button android:id="@+id/stt_button" android:layout_width="45dp" android:layout_height="50dp" android:background="@drawable/microphone"/> <edittext android:id="@+id/chat_msg" android:inputtype="text" android:layout_width="0dp" android:layout_height="40dp" android:layout_weight="1" /> <button android:id="@+id/send_button" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_gravity="center_vertical" android:text="@string/send_button" /> </linearlayout> </linearlayout>

list item layout:

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:weightsum="1.0" android:layout_weight="1" android:layout_height="wrap_content"> <textview android:id="@+id/lefttext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="10dip" android:layout_marginright="10dip" android:layout_margintop="10dip" android:layout_marginbottom="5dip" android:layout_alignparentleft="true" android:maxwidth="250dip"/> <textview android:id="@+id/righttext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="10dip" android:layout_marginright="10dip" android:layout_margintop="10dip" android:layout_marginbottom="5dip" android:layout_alignparentright="true" android:maxwidth="250dip"/> </relativelayout>

this code within getview method of custom array adapter:

view view = convertview; if(view == null){ view = minflater.inflate(r.layout.list_item, null); } resources res = getcontext().getresources(); drawable bubbleschat = res.getdrawable(r.drawable.bubbles_chat); drawable bubblesresponse = res.getdrawable(r.drawable.bubbles_response); textview left = (textview) view.findviewbyid(r.id.lefttext); textview right = (textview) view.findviewbyid(r.id.righttext); string txt = super.getitem(position); if(txt.startswith("s:")) { left.settext(getitem(position)); left.setbackgrounddrawable(bubbleschat); right.settext(""); right.setbackgrounddrawable(null); } else { right.settext(getitem(position)); right.setbackgrounddrawable(bubblesresponse); left.settext(""); left.setbackgrounddrawable(null); } homecoming view;

android android-layout

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -