androidcardviewlayout-gravity

How to set layout_gravity to a cardview in android dynamically


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>

Solution

  • 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);