1

I am trying to create a chatting like functionality in my app and for the chat text i am using a cardview. I want the cardview to be aligned to the right side of the screen for sent messages. But when i set gravity attribute to the parent of the cardview it works on all other attributes but cardview won't budge. Plz tell me how to set layout_gravity to cardview (dynamic approach will be appriciated). Thanx.

Here is my model_msg.xml file and the settings for sent msgs are done in the adapter class:

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_marginEnd="40dp"
    android:layout_marginTop="6dp"
    android:layout_marginBottom="6dp"
    android:layout_height="wrap_content"
    android:id="@+id/rl_msg"
    android:layout_gravity="center">

        <TextView
            android:paddingStart="8dp"
            android:text="Sender Name"
            android:maxLines="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_senderName"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentEnd="true" />

        <android.support.v7.widget.CardView
            android:layout_width="wrap_content"
            card_view:cardBackgroundColor="@color/cBgrMsg"
            card_view:cardElevation="6dp"
            android:layout_height="wrap_content"
            android:id="@+id/card_msgText"
            android:layout_below="@id/tv_senderName"
            card_view:cardCornerRadius="6dp">

        <TextView
        android:text="The message will come here which is gonna be a lot larger than this textView can handle and at that time to avoid any bugs or not to discard and functionalities I would have to utilize my mind a lot which is gonna be so tiresome and annoying that I  would think about leaving this app in middle without finishing it. Damn."
        android:padding="6dp"
        android:textSize="16sp"
        android:maxLength="500"
        android:layout_width="wrap_content"
        android:textColor="@color/cWhite"
        android:layout_height="wrap_content"
        android:id="@+id/tv_msg"
        android:layout_below="@id/tv_senderName" />

        </android.support.v7.widget.CardView>

    <TextView
        android:text="Nothing to show."
        android:padding="4dp"
        android:background="#0000"
        android:textSize="10sp"
        android:textStyle="italic"
        android:layout_width="wrap_content"
        android:gravity="start"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:textColor="#999999"
        android:layout_height="wrap_content"
        android:id="@+id/tv_sentAt"
        android:layout_below="@id/card_msgText" />
</RelativeLayout>
santosh kumar
  • 2,952
  • 1
  • 15
  • 27
Ashu
  • 2,970
  • 1
  • 19
  • 31
  • I think this is a duplicate. Check the link bellow. http://stackoverflow.com/questions/8049620/how-to-set-layout-gravity-programmatically – a.stefan Feb 03 '17 at 13:48

3 Answers3

2

Wrap your card view in a LinearLayout. Then set the gravity of the linear layout using setGravity(Gravity gravity) on the LinearLayout as seen below. Mind you I am using a RecyclerView to repeat the messages.

message_row_item.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/message_view">

        <android.support.v7.widget.CardView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/message_text_view" />
        </android.support.v7.widget.CardView>
    </LinearLayout>

UI Logic:

    LinearLayout viewMessage = itemView.findViewById(R.id.message_view);

    if (messageList.get(position).getPeerId() == USER_PEER_ID) {
        viewMessage.setGravity(LEFT);
    } else {
        viewMessage.setGravity(RIGHT);
    }

Note:

I tried to access the CardView's LayoutParams and the setForegroundGravity on the cardview as directed by other answers, but had no such luck so I implemented it as seen above.

S4NT14G0
  • 164
  • 2
  • 6
0

Try this way:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
yourCardViewHere.setLayoutParams(params);
Valentino
  • 2,125
  • 1
  • 19
  • 26
  • It gives me the error : java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams – Ashu Feb 03 '17 at 13:56
  • Sorry I didn't see that was a RelativeLayout, check my udapte – Valentino Feb 03 '17 at 13:59
  • 1
    Well i got answer somewhere else but thanx anyway. Its better to choose your answer lol. – Ashu Feb 03 '17 at 14:15
0

I got the right answer guys here:https://stackoverflow.com/a/35241091/4836759

Thanx anyways guys.

   RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);

layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END);
cardView.setLayoutParams(layoutParams);
Community
  • 1
  • 1
Ashu
  • 2,970
  • 1
  • 19
  • 31