Set Gravity Programmatically For Item In Recycerview Android
I am using RecyclerView Android to make a chat line with left/right message box. I want to set gravity to item of RecyclerView. In normally way, I cast itemView to LinearLayout and
Solution 1:
Problem solved. Simply, I using
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
instead of
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) holder.llGrandParent.getLayoutParams();
And then set gravity for LinearLayout Params normally.
Solution 2:
I did that by setting the gravity on the view inside the item's layout.
LinearLayout.LayoutParamsparamsMsg=newLinearLayout.
LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
if (position%2 == 0){
holder.bodyView.setBackgroundResource(R.drawable.outgoing_messages);
paramsMsg.gravity = Gravity.END;
} else {
holder.bodyView.setBackgroundResource(R.drawable.incoming_messages);
holder.bodyView.setTextColor(0xf08888);
paramsMsg.gravity = Gravity.START;
}
holder.bodyView.setLayoutParams(paramsMsg);
bodyView is the TextView inside the item's Layout and the item width is "match_parent".
Post a Comment for "Set Gravity Programmatically For Item In Recycerview Android"