Search code examples
androidlistviewarraylistandroid-listviewlistadapter

Android - custom list view item always sits to the left


Hello

I've built a chat app that uses a listview with a custom list item xml to display the chat messages on screen.
My application will show a textview on the left or the right depending on who the message is from. it does this by setting the text to either a right or left textview and then showing and hiding the relevant views.

My problem is that no matter how I've tried to arrange the views in my custom list item, they will always appear on the left hand side of the screen. I've tried setting the gravity in my xml, setting it programmatically, using fill_parent, using weighting views. it just won't budge from the left! does anyone have a solution to this or can see where I'm going wrong?

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

        if (convertView == null) {

            convertView = LayoutInflater.from(getContext()).inflate(R.layout.chat_item, parent, false);

            final ViewHolder holder = new ViewHolder();

            holder.chatRight = (TextView)convertView.findViewById(R.id.tvChatBubbleRight);
            holder.chatLeft = (TextView)convertView.findViewById(R.id.tvChatBubbleLeft);

            convertView.setTag(holder);
        }

        final ParseChat parseChat = (ParseChat)getItem(position);

        final ViewHolder holder = (ViewHolder)convertView.getTag();

        final boolean isMe = parseChat.getSender().equals(mUserId);

        // Show-hide image based on the logged-in user.
        // Display the profile image to the right for our user, left for other users.
        if (isMe) {

            holder.chatRight.setText(parseChat.getMessage());

            holder.chatRight.setGravity(Gravity.RIGHT);
            holder.chatRight.setVisibility(View.VISIBLE);
            holder.chatLeft.setVisibility(View.GONE);
        }

        else {

            holder.chatLeft.setText(parseChat.getMessage());

            holder.chatLeft.setGravity(Gravity.LEFT);
            holder.chatLeft.setVisibility(View.VISIBLE);
            holder.chatRight.setVisibility(View.GONE);
        }

        return convertView;
    }

chat_item.xml

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

    <TextView
        android:textSize="18sp"
        android:id="@+id/tvChatBubbleLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/chat_bubble_left"
        android:layout_gravity="left"
        android:layout_weight="1"
        android:gravity="left">
    </TextView>

    <TextView
        android:textSize="18sp"
        android:id="@+id/tvChatBubbleRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/chat_bubble_right"
        android:layout_gravity="right"
        android:gravity="right"
        android:layout_weight="1">
    </TextView>

</LinearLayout>

thank you


Solution

  • Thanks for the help everyone.
    I managed to do this with the help fo this post left and right alignment rows inside Listview?



    ~ in chat_item.xml, layout_width attribute of the parent LinearLayout should be set to wrap_content instead of match_parent

    ~ in chat_activity.xml, layout_width attribute of ListView should be set to match_parent. not wrap_content