title: How to design chat talking layout has leftside and rightside when use Firebase?
body: <p>I use Firebase realtime databse to complete chat app.</p>
  
  <p>I want to design when the name is <code>Tom</code>, the textView will show on right side side, others will show on left side.</p>
  
  <p>Here's my layout: two textView design for left and right:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
  
      &lt;Button
          android:background="@drawable/ic_send"
          android:id="@+id/button2"
          android:layout_width="50dp"
          android:layout_height="50dp"
          android:layout_alignParentBottom="true"
          android:layout_alignParentEnd="true"
          android:layout_alignParentRight="true"
          /&gt;
  
      &lt;EditText
          android:id="@+id/editText2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:layout_toLeftOf="@+id/button2"
          android:layout_toStartOf="@+id/button2"
          android:ems="10"
          android:inputType="textPersonName"
          android:text="Name" /&gt;
  
      &lt;ScrollView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:layout_alignParentTop="true"&gt;
  
          &lt;LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"&gt;
  
              &lt;TextView
                  android:id="@+id/textLeft"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:text="It's left &gt;" /&gt;
  
              &lt;TextView
                  android:id="@+id/textRight"//It's for Tom
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:text="It's right &gt;"/&gt;
  
          &lt;/LinearLayout&gt;
  
      &lt;/ScrollView&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Here is my chat activity class:</p>
  
  <pre><code>    public class Chat_Room extends AppCompatActivity {
  
          private Button button2;
          private TextView textLeft,textRight;
          private EditText editText2;
  
          private String room_name, user_name;
          private String myName;
  
          private DatabaseReference root;
          private DatabaseReference rootName;
          private String temp_key;
  
          @Override
          protected void onCreate(@Nullable Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.chat_room);
  
              button2 = (Button) findViewById(R.id.button2);
              textLeft = (TextView) findViewById(R.id.textLeft);
              textRight = (TextView) findViewById(R.id.textRight);
              editText2 = (EditText) findViewById(R.id.editText2);
  
              user_name = getIntent().getExtras().getString("user_name").toString();
              room_name = getIntent().getExtras().getString("room_name").toString();
  
              setTitle("Room name:" + room_name);
  
              root = FirebaseDatabase.getInstance().getReference().child(room_name);//
  
              Log.d("Name is : ",rootName+"");
  
  //send text msg
              button2.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
  
                      Map&lt;String, Object&gt; map = new HashMap&lt;&gt;();
                      temp_key = root.push().getKey();
                      root.updateChildren(map);
  
                      DatabaseReference message_root = root.child(temp_key);
                      Map&lt;String, Object&gt; map2 = new HashMap&lt;&gt;();
                      map2.put("name", user_name);
                      map2.put("msg", editText2.getText().toString());
  
                      message_root.updateChildren(map2);
                  }
              });
  
              root.addChildEventListener(new ChildEventListener() {
                  @Override
                  public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                      myName= dataSnapshot.child("name").getValue(String.class);
                      Log.d("Name is : ",myName);
  
                          append_Right_chat_conversation(dataSnapshot);
  
                  }
  
                  @Override
                  public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                      //append_right_chat_conversation(dataSnapshot);
                  }
  
  
                  @Override
                  public void onChildRemoved(DataSnapshot dataSnapshot) {
  
                  }
  
                  @Override
                  public void onChildMoved(DataSnapshot dataSnapshot, String s) {
  
                  }
  
                  @Override
                  public void onCancelled(DatabaseError databaseError) {
  
                  }
              });
  
          }
  
          private String chat_msg, chat_user_name;
  
  
          private void append_Right_chat_conversation(DataSnapshot dataSnapshot) {
              Iterator i = dataSnapshot.getChildren().iterator();
  
              chat_msg=(String) ((DataSnapshot)(i.next())).getValue();
              chat_user_name=(String) ((DataSnapshot)(i.next())).getValue();
  
              textRight.append(chat_user_name+":"+chat_msg+" \n");
  
      //        textLeft.setGravity(Gravity.RIGHT);
  
          }
      }
  </code></pre>
  
  <p>Here's my Database Structure:</p>
  
  <p><a href="https://i.stack.imgur.com/t3Jp5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/t3Jp5.png" alt="enter image description here"></a></p>
  
  <p>I try to use this code on onCreatView:</p>
  
  <pre><code>new Thread(new Runnable() {
              @Override
              public void run() {
  
                  if (myName.equals("Tom")) {
                      textRight.setGravity(Gravity.RIGHT);
                  } else {
  
                  }
  
              }
          }).start();
  </code></pre>
  
  <p>But it's not working. :(</p>
  
  <p>Can anyone can give some suggestions or a sample? Thanks in advance.</p>
  
  <p>Edit- It shows like this:</p>
  
  <p><img src="https://i.stack.imgur.com/Gaszh.png" alt="![enter image description here">]<a href="https://i.stack.imgur.com/Gaszh.png" rel="nofollow noreferrer">2</a></p>