0

I'm trying to make a chat app, and having a trouble with setting gravity of custom view in the RecyclerView. I've been searching and trying a whole day, but i couldn't make, it always shows like this(ScreenShot).It should be located right of screen not left. But in Android Studio Preview, It shows correctly.Im attaching my source.. please tell me what's wrong..

  1. In MainActivity.xml (RecyclerView)

    ......
    <RelativeLayout
        android:background="#9cbad8"
        android:layout_weight="70"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        android:layout_alignParentRight="true"
        android:paddingBottom="5dp"
        android:listSelector="@drawable/bg_transparent"
        android:cacheColorHint="@android:color/transparent"
        android:paddingTop="50dp"
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    </RelativeLayout>
    .......
    

2.In MessageItem.xml (I set graivity Right, and screenshot shows correctly)

<LinearLayout
        android:id="@+id/my_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
            <LinearLayout
                android:id="@+id/my_normal_layout"
                android:orientation="horizontal"
                android:gravity="end"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/my_time"
                        android:textSize="10sp"
                        android:layout_marginRight="5dp"
                        android:layout_gravity="bottom"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="오후 15:15"/>

                    <TextView
                        android:textAlignment="center"
                        android:layout_marginTop="5dp"
                        android:layout_marginRight="5dp"
                        android:maxWidth="200dp"
                        android:textSize="14sp"
                        android:lineSpacingMultiplier="1.2"
                        android:gravity="center_vertical"
                        android:paddingLeft="10dp"
                        android:paddingRight="15dp"
                        android:paddingTop="8dp"
                        android:paddingBottom="8dp"
                        android:background="@drawable/bubble_my"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="#000000"
                        android:text="this is Test"
                        android:id="@+id/my_normal_text" />
            </LinearLayout>
</LinearLayout>

Android Layout Preview shows well(ScreenShot)

  1. My RecyclerViewHolder

    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout myLayout;
        LinearLayout myNormarLayout;
        LinearLayout myMoreLayout;
    
        public ViewHolder(View v) {
            super(v);
            myLayout = (LinearLayout)v.findViewById(R.id.my_layout);
            myNormarLayout = (LinearLayout)v.findViewById(R.id.my_normal_layout);
            myMoreLayout = (LinearLayout)v.findViewById(R.id.my_more_layout);
        }
    }
    
  2. Also in my RecyclerViewAdapter, I changed Gravity Again..

    ....
    holder.myNormarLayout.setVisibility(View.VISIBLE);
    holder.myNormarLayout.setGravity(Gravity.Right);
    ....
    

I had tried almost of things from searching, but they didn't work for this.. please... help me

MWP
  • 35
  • 2
  • 8

1 Answers1

0

You could use the Constraint Layout for better performance.

In your case maybe adding a weighted view between the time and text could help, try this

<LinearLayout
    android:id="@+id/my_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
        <LinearLayout
            android:id="@+id/my_normal_layout"
            android:orientation="horizontal"
            android:gravity="end"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/my_time"
                    android:textSize="10sp"
                    android:layout_marginRight="5dp"
                    android:layout_gravity="bottom"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="오후 15:15"/>
                <View
                      android:layout_height="0dp"
                      android:layout_width="0dp"
                      android:layout_weight="0"
                      />
                <TextView
                    android:textAlignment="center"
                    android:layout_marginTop="5dp"
                    android:layout_marginRight="5dp"
                    android:maxWidth="200dp"
                    android:textSize="14sp"
                    android:lineSpacingMultiplier="1.2"
                    android:gravity="center_vertical"
                    android:paddingLeft="10dp"
                    android:paddingRight="15dp"
                    android:paddingTop="8dp"
                    android:paddingBottom="8dp"
                    android:background="@drawable/bubble_my"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#000000"
                    android:text="this is Test"
                    android:id="@+id/my_normal_text" />
        </LinearLayout>

Boldijar Paul
  • 4,925
  • 7
  • 39
  • 83
  • My bad, I changed something on the parent layout, check again copy paste, all I changed is adding the View between the 2 text views. – Boldijar Paul Nov 08 '16 at 22:26
  • Tahnks but i just tried , it doesn't work it is still located to left .. – MWP Nov 08 '16 at 22:31
  • My Main Activity.xml shows like this, [link](http://imgur.com/a/Ppr8K) and is that related to this problem?? – MWP Nov 08 '16 at 22:34
  • Don't think so. Maybe put the whole project on github, and I can try to fork it, and do a fix. – Boldijar Paul Nov 09 '16 at 07:19
  • Thank you so much sir!! I just uploaded on Github. When you have time, please check, Thank you again! [uploadedLink](https://github.com/pmw188/test) – MWP Nov 10 '16 at 09:03
  • Thank you sosososososo much!! – MWP Nov 10 '16 at 12:23