Reputation: 415
i am working with recyclerview and i want to set gravity of a Linearlayout left or right in recyclerview .but my Linearlayout alwayas set in left my code is shown below.
@Override
public void onBindViewHolder(MessageWindowHolder holder, int position) {
if(notificationDescriptions.get(position).getIsRecieve())
{
holder.linearLayout.setGravity(Gravity.LEFT);
holder.messageTextview.setText(notificationDescriptions.get(position).getMessage());
holder.messageTextview.setBackground(notificationDescriptionActivity.getResources().getDrawable((R.drawable.bubble_a)));
}
else {
holder.linearLayout.setGravity(Gravity.RIGHT);
holder.messageTextview.setText(notificationDescriptions.get(position).getMessage());
holder.messageTextview.setBackground(notificationDescriptionActivity.getResources().getDrawable(R.drawable.bubble_b));
}
}
notification_message.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/singleMessageContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/singleMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:background="@drawable/bubble_a"
android:paddingLeft="10dip"
android:text="Hello bubbles!"
android:textColor="@android:color/primary_text_light" />
</LinearLayout>
</LinearLayout>
viewholder.java
public class MessageWindowHolder extends RecyclerView.ViewHolder {
protected LinearLayout linearLayout;
protected TextView messageTextview;
public MessageWindowHolder(View view, List<NotificationDescription> notificationDescriptions) {
super(view);
this.linearLayout = (LinearLayout)view.findViewById(R.id.singleMessageContainer);
this.messageTextview = (TextView)view.findViewById(R.id.singleMessage);
}
}
Main layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.mindefy.kidster.activity.NotificationDescriptionActivity"
tools:showIn="@layout/activity_notification_description">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/notificationRecyclerViewParent"
android:layout_centerHorizontal="true"
android:layout_marginBottom="80dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="serif"
android:id="@+id/sendMessageEdittext"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignTop="@+id/sendMessageEdittext"
android:layout_alignParentRight="true"
android:id="@+id/sendMessageReplyButton"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
Upvotes: 2
Views: 2809
Reputation: 216
To place the view on the right side of the RecyclerView, you must wrap the layout of the outgoing chat message in a simple layout and set the child's layout_gravity to "end":
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/singleMessageContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null">
<TextView
android:id="@+id/singleMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_margin="5dp"
android:background="@drawable/bubble_a"
android:paddingLeft="10dp"
android:text="Hello bubbles!"
android:textColor="@android:color/primary_text_light" />
</FrameLayout>
Upvotes: 0
Reputation: 53
SetGravity
of LinearLayout
sets layout_gravity
of LinearLayout
in his parent. Change singleMessageContainer
width from fill_parent
to wrap_content
. And this will work
Update : use Gravity.End
and Gravity.Start
Upvotes: 1
Reputation: 43
Try Below code:
LinearLayout.LayoutParams params=(LinearLayout.LayoutParams)holder.linearLayout.getLayoutParams(); params.gravity= Gravity.LEFT;
Hope it will work :)GlbMP
Upvotes: 1
Reputation: 61
Your code is well, but I think try this..
LayoutParams lp = new LayoutParams();
lp.gravity= Gravity.CENTER_HORIZONTAL;
holder.linearLayout.setLayoutParams(lp);
UPDATE : Another way to do this :
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.weight = 1.0f;
params.gravity = Gravity.CENTER;
holder.linearLayout.setLayoutParams(params);
Upvotes: 1