title: How to set gravity in programmatically in a relativelayout
body: <p>How to set gravity in programmingcally in a relativelayout. I have a XML layout with name <strong>chat_viewer_message.xml</strong> as below:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/background"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:paddingRight="4dip"
      android:paddingBottom="4dip"
      android:gravity="left"
      android:background="@drawable/chat_bg_in"&gt;
  
      &lt;ImageView
          android:id="@+id/avatar"
          android:layout_width="32dip"
          android:layout_height="32dip"
          android:layout_marginLeft="4dip"
          android:src="@drawable/avatar_1_1"
          /&gt;
  
      &lt;TextView
          android:id="@+id/text"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_toRightOf="@id/avatar"
          android:paddingLeft="4dip"
          /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>And code view in programming is as below:</p>
  
  <pre><code>@Override
      public View getView(int position, View convertView, ViewGroup parent) {
          final int type = getItemViewType(position);
          final View view;
          if (convertView == null) {
              final int resource = R.layout.chat_viewer_message;
              view = activity.getLayoutInflater()
                      .inflate(resource, parent, false);
              if (type == TYPE_MESSAGE)
                  ((TextView) view.findViewById(R.id.text)).setTextAppearance(
                          activity, appearanceStyle);
          } else
              view = convertView;
  
  
  
          final MessageItem messageItem = (MessageItem) getItem(position);
          String name;
          final String account = messageItem.getChat().getAccount();
          final String user = messageItem.getChat().getUser();
          final String resource = messageItem.getResource();
          final boolean incoming = messageItem.isIncoming();
          final String server_host = view.getResources().getString(
                  R.string.server_host);
          if (isMUC) {
              name = resource;
          } 
  
  
          if (incoming) {
              view.setBackgroundResource(R.drawable.chat_bg_in);
          } else {
              // I WANT TO GRAVITY TO RIGHT. HOW TO CODE? THANKS
              view.setBackgroundResource(R.drawable.chat_bg_out);
          }
  
          return view;
  }
  </code></pre>
  
  <p>See gravity at the relativelayout with id = background, the default gravity is LEFT, So I want to if !incoming the value gravity at relativelayout be RIGHT instead LEFT.</p>
  
  <p>Thanks.</p>