android - How to set gravity to layout programmatically? -


i creating chat application in need align chat messages accordingly.i need align user right side , others left side.i have listview in messages displayed.for now,i using bundled json getting data.here code

this appending string message typed user checked , splitted inorder recognize messages user.

final edittext et = (edittext)findviewbyid(r.id.edittext1); final button imb=(button)findviewbyid(r.id.btn_send);             imb.setonclicklistener(new onclicklistener()             {              @override              public void onclick(view arg0)               {                    string str = et.gettext().tostring()+":aeiou";                   web.add(str);                   scrollmylistviewtobottom();                   et.settext(" ");                   adapter.notifydatasetchanged();                } 

after that, splitting string , stored in array.if message user.ie,if has appended string,then gravity of layout need set right,else left. used code in adapter class this

    mychat=web.get(position); if(mychat.contains(":")) {     string[] splitted = mychat.split(":");     string chats;      if(splitted[1].equalsignorecase("aeiou"))     {         chats=splitted[0];        toast.maketext(context, "you entered...."+chats, toast.length_short).show();        linearlayout my_layout = (linearlayout) rowview.findviewbyid(r.id.main_layout);         linearlayout.layoutparams params = new linearlayout.layoutparams(                 layoutparams.wrap_content, layoutparams.wrap_content);        params.gravity = gravity.right;        my_layout.setlayoutparams(params);         //my_layout.setgravity(gravity.right);        txttitle.settext(chats);     }    } else     {     txttitle.settext(web.get(position));     }     txttitle.setbackgroundresource(r.drawable.test); 

what wrong in ?please help...

here xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/main_layout"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="#2a2a34"     android:padding="3dp"     android:orientation="vertical" >      <linearlayout         android:layout_width="match_parent"         android:layout_height="wrap_content" >           <relativelayout         android:id="@+id/chat_icon_layout"             android:layout_width="match_parent"             android:layout_weight="4"             android:layout_height="match_parent" >              <imageview                 android:id="@+id/img"                 android:layout_width="60dp"                 android:layout_height="60dp"                 android:layout_centerhorizontal="true"                 android:layout_centervertical="true"                 android:src="@drawable/white" />              <imageview                 android:id="@+id/img1"                 android:layout_width="55dp"                 android:layout_height="55dp"                 android:layout_centerhorizontal="true"                 android:layout_centervertical="true"                 android:src="@drawable/abs__ab_bottom_solid_light_holo" />              </relativelayout>          <linearlayout             android:id="@+id/chat_text_layout"             android:layout_width="match_parent"             android:layout_height="match_parent"             android:padding="0dp"             android:layout_weight="1" >                      <textview                      android:id="@+id/txt"                      android:layout_width="fill_parent"                      android:layout_height="wrap_content"                      android:text="message"                      android:textappearance="?android:attr/textappearancesmall"                      android:textcolor="#ffffff" />              </linearlayout>       </linearlayout>  </linearlayout> 

since want align chat messages left , right therefore need align text messages i.e text view. therefore need do

if(...)   txttitle.settext(chats); txttitle.setgravity(gravity.right);     }    } else     {     txttitle.settext(web.get(position)); txttitle.setgravity(gravity.left);     } 

Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

how to receive file in java(servlet/jsp) -