0

in android i am trying to change the layout base on who the message is from. if the message is from me then display the layout mymessage.xml to the right else display message.xml to the left. i used if condition, but i don't know how to display one layout to the right and the second to the left,

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    // TODO
    View messageView = null;

    // Get a reference to the LayoutInflater. This helps construct the
    // view from the layout file
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    // Change the layout based on who the message is from
    if (messages.get(position).fromMe()) {


        messageView = inflater.inflate(R.layout.mymessage , parent, false);

        //initialization of 2 textView, message and time
        TextView timeView = (TextView) messageView.findViewById(R.id.mytimeTextView);
        timeView.setText(messages.get(position).getTime());

        TextView msgView = (TextView) messageView.findViewById(R.id.mymessageTextView);
        msgView.setText(messages.get(position).getMessage());
    } else {


        messageView = inflater.inflate(R.layout.message , parent, true);

        //initialization of 2 textView, message and time
        TextView timeView = (TextView) messageView.findViewById(R.id.timeTextView);
        timeView.setText(messages.get(position).getTime());

        TextView msgView = (TextView) messageView.findViewById(R.id.messageTextView);
        msgView.setText(messages.get(position).getMessage());

    }

    return messageView;
    }
kane kayp
  • 9
  • 1

5 Answers5

1

For left.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/msgr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_marginBottom="5dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/textview"
        android:paddingBottom="8dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="5dp"
        android:text="Sampleleft"
        android:textColor="#000" />
</RelativeLayout>

For right.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/msgr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="20dp"
        android:background="@drawable/textview"
        android:paddingBottom="5dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="5dp"
        android:text="Sample"
        android:textColor="#000" />
</LinearLayout>

just replace this // Change the layout based on who the message is from

 if (messages.get(position).fromMe()) {


        messageView = inflater.inflate(R.layout.left , parent, false);

        //initialization of 2 textView, message and time
        TextView timeView = (TextView) messageView.findViewById(R.id.mytimeTextView);
        timeView.setText(messages.get(position).getTime());

        TextView msgView = (TextView) messageView.findViewById(R.id.mymessageTextView);
        msgView.setText(messages.get(position).getMessage());
    } else {


        messageView = inflater.inflate(R.layout.right , parent, true);

        //initialization of 2 textView, message and time
        TextView timeView = (TextView) messageView.findViewById(R.id.timeTextView);
        timeView.setText(messages.get(position).getTime());

        TextView msgView = (TextView) messageView.findViewById(R.id.messageTextView);
        msgView.setText(messages.get(position).getMessage());

    }
matin sayyad
  • 575
  • 3
  • 10
  • thank you !! but what i want all the layout are hidden, but is i type the message in messagetextView if the message is me who typed it, then mymessage.xml will be visible to the left, while is from another one message.xml will be on right. – kane kayp Mar 16 '18 at 10:54
  • bro it will give you same output as u expected – matin sayyad Mar 16 '18 at 10:56
1

try this layout for mymessage.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content">

<TextView
    android:id="@+id/mytimeTextView"
    android:alpha="0.5"
    android:layout_alignParentRight="true"
    android:text="5:00 pm"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<TextView
    android:layout_below="@+id/mytimeTextView"
    android:id="@+id/mymessageTextView"
    android:layout_alignParentRight="true"
    android:text="mymessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</RelativeLayout>

and similarly this for message.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content">

<TextView
    android:id="@+id/timeTextView"
    android:alpha="0.5"
    android:layout_alignParentLeft="true"
    android:text="5:00 pm"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<TextView
    android:layout_below="@+id/timeTextView"
    android:id="@+id/messageTextView"
    android:layout_alignParentLeft="true"
    android:text="yourmessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</RelativeLayout>
Navneet Krishna
  • 5,009
  • 5
  • 25
  • 44
1

for message.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/receive_ll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="50dp"
        android:layout_marginRight="50dp"
        android:background="@drawable/message_box"
        android:orientation="vertical"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:visibility="visible">

        <TextView
            android:id="@+id/tv_receiver_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="User 1"
            android:textColor="@color/NavDrawerTextColor"
            android:textSize="14dp"/>

        <TextView
            android:id="@+id/tvmessagetext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="Hi"
            android:textColor="@color/MessageText"
            android:textSize="14dp"/>

        <TextView
            android:id="@+id/timestamp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="5dp"
            android:text="04:16 pm"
            android:textSize="10dp"/>


    </LinearLayout>

    <LinearLayout
        android:id="@+id/send_ll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="50dp"
        android:layout_marginStart="50dp"
        android:background="@drawable/message_sender_box"
        android:orientation="vertical"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:visibility="visible">

        <TextView
            android:id="@+id/tvmessagetext_send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="user 2"
            android:textColor="@color/White"
            android:textSize="14dp"/>

        <TextView
            android:id="@+id/timestamp_send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="5dp"
            android:text="Hello"
            android:textColor="@color/White"
            android:textSize="10dp"/>

    </LinearLayout>

</RelativeLayout>

for java class: getview()

    convertView = inflater.inflate(R.layout.message,parent, false);
    if (messages.get(position).fromMe()) {
    // for reference receive_ll is GONE and send_ll layout is VISIBLE
    } else {
    // for reference receive_ll layout is VISIBLE and send_ll layout GONE
    }
Manikandan K
  • 911
  • 9
  • 21
0

Just make it simple .

  1. create single layout contain two category
  2. set the data's in the fields
  3. And VISIBLE GONE the layout by the type

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
    
    <LinearLayout
        android:id="@+id/sender_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        //sender side
    </LinearLayout>
    
    <LinearLayout
        android:id="@+id/reciver_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        //reciver side
    </LinearLayout></LinearLayout>
    

if the message type is sender means visible thesender_layout else visible the reciver_layout

Ramkumar.M
  • 681
  • 6
  • 21
0

when you getting myMessage that time show mymessage layout other layout will hide like make below codition ...

if (messages.get(position).fromMe()) {


    myMessageView = inflater.inflate(R.layout.mymessage , parent, false);
    myMessageView.setVisibility(View.VISIBLE);

    //initialization of 2 textView, message and time
    TextView timeView = (TextView) messageView.findViewById(R.id.mytimeTextView);
    timeView.setText(messages.get(position).getTime());

    TextView msgView = (TextView) messageView.findViewById(R.id.mymessageTextView);
    msgView.setText(messages.get(position).getMessage());
    messageView.setVisibility(View.GONE);
} else {


    messageView = inflater.inflate(R.layout.message , parent, true);
    messageView.setVisibility(View.VISIBLE);

    //initialization of 2 textView, message and time
    TextView timeView = (TextView) messageView.findViewById(R.id.timeTextView);
    timeView.setText(messages.get(position).getTime());

    TextView msgView = (TextView) messageView.findViewById(R.id.messageTextView);
    msgView.setText(messages.get(position).getMessage());
    myMessageView.setVisibility(View.GONE);
}