Android, How to get/set layout parameters? -
in application have conversation room. of messages should aligned right while rest should left aligned.
single row of list this:
<?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="wrap_content" android:id="@+id/llcontainer" > <linearlayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="@drawable/speech_bubble_orange" android:id="@+id/llgroup" > <textview android:id="@+id/message_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:shadowcolor="@color/message_textshadow" android:shadowdx="1" android:shadowdy="1" android:text="medium text" android:textcolor="@color/message_textcolor" android:textsize="20sp" /> <textview android:id="@+id/message_date" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/temp_date" android:gravity="right" android:layout_marginleft="4dp" android:layout_marginright="4dp"/> </linearlayout> </linearlayout>
getview() of adapter this:
@override public view getview(final int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { convertview = myinflater.inflate(r.layout.list_message_row, null); holder = new viewholder(); holder.tvmessagebody = (textview) convertview.findviewbyid(r.id.message_text); holder.tvmessagedate = (textview) convertview.findviewbyid(r.id.message_date); holder.llcontainer = (linearlayout) convertview.findviewbyid(r.id.llcontainer); holder.llgroup = (linearlayout) convertview.findviewbyid(r.id.llgroup); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.tvmessagebody.settext(messagelist.get(position).getmessagebody()); string date = messagelist.get(position).getsentdate(); holder.tvmessagedate.settext(formatfancieddate(date)); linearlayout.layoutparams params = (linearlayout.layoutparams) holder.llcontainer.getlayoutparams(); try { if(messagelist.get(position).ismine()) { holder.llgroup.setbackgroundresource(r.drawable.speech_bubble_green); params.gravity = gravity.right; } //if not mine sender show orange background , align left else { holder.llgroup.setbackgroundresource(r.drawable.speech_bubble_orange); params.gravity = gravity.left; } holder.llcontainer.setlayoutparams(params); } catch (nullpointerexception e) { e.printstacktrace(); } return convertview; }
i think should fine when run application nullpointerexception happens , points params.gravity = gravity.right;
or params.gravity = gravity.left;
(depends on ismine() method).
what think? mistake? suggestion appreciated.
use relativelayout params , set gravity this, work.
relativelayout.layoutparams params = new relativelayout.layoutparams(relativelayout.layoutparams.wrap_content, relativelayout.layoutparams.wrap_content); params.addrule(relativelayout.align_parent_right, relativelayout.true);
this working me in application.
Comments
Post a Comment