title: How to set gravity to layout programmatically?
body: <p>I am creating a chat application in which i need to align the chat messages accordingly.I need to align those from the user to right side and others to the left side.I have a listview in which the messages are displayed.For now,i am using bundled json for getting data.Here is my code</p>
  
  <p>This is for appending a string to the message typed by the user which is checked and splitted inorder to recognize messages from the user.</p>
  
  <pre><code>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();
  
  
               }
  </code></pre>
  
  <p>After that, i am splitting the string and stored in an array.If the message is from user.ie,if it has the appended string,then the gravity of the layout need to be set to RIGHT,else LEFT.
  I used this code in my adapter class for this</p>
  
  <pre><code>    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);
  </code></pre>
  
  <p>What is wrong in this ?Please help...</p>
  
  <p>Here is my xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;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" &gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content" &gt;
  
  
          &lt;RelativeLayout
          android:id="@+id/chat_icon_layout"
              android:layout_width="match_parent"
              android:layout_weight="4"
              android:layout_height="match_parent" &gt;
  
              &lt;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" /&gt;
  
              &lt;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" /&gt;
  
              &lt;/RelativeLayout&gt;
  
          &lt;LinearLayout
              android:id="@+id/chat_text_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:padding="0dp"
              android:layout_weight="1" &gt;
  
                      &lt;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" /&gt;
  
              &lt;/LinearLayout&gt;
  
  
      &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>