title: Android, How to get/set layout parameters?
body: <p>In my application I have a conversation room. All of my messages should be aligned to right while the rest should be left aligned.</p>
  
  <p>Single row of my list is like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/llContainer" &gt;
  
      &lt;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" &gt;
  
              &lt;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" /&gt;
  
              &lt;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"/&gt;
      &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>getView() of my adapter is like this:</p>
  
  <pre><code>@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 then it is from sender to show orange background and align to 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;
  }
  </code></pre>
  
  <p>I think everything should be fine however when I run the application NullPointerException happens and points to <code>params.gravity = Gravity.RIGHT;</code> or <code>params.gravity = Gravity.LEFT;</code> (depends on isMine() method).</p>
  
  <p>What you think? Where is my mistake? 
  Any suggestion would be appreciated.</p>