I'm trying to create a chat view(sent text,received text). The background drawable(box) is properly aligned to one side. When i scroll, it extends the box to fit the whole width. Not sure why this is happening. Any ideas? The problem i'm talking aboutHow i actually want it(this is before scrolling)
My custom adapter class
public class DisplayMessageAdapter extends ArrayAdapter
{Context context;int sentResource, rcvdResource;ArrayList messages = null;public DisplayMessageAdapter(Context context, int sentResource, int rcvdResource, ArrayList messages) { super(context, sentResource, rcvdResource, messages); this.context = context; this.sentResource = sentResource; this.rcvdResource = rcvdResource; this.messages = messages;}@SuppressLint("NewApi")@Overridepublic View getView(int position, View convertView, ViewGroup parent) {View row = convertView;Message message = messages.get(position);Holder holder = new Holder(); if(row == null) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); holder = new Holder(); row = inflater.inflate(R.layout.message_layout, parent, false); holder.content = (TextView) row.findViewById(R.id.message_content); row.setTag(holder); } else { holder = (Holder) row.getTag(); } if(message.sent != null) { RelativeLayout.LayoutParams params = (LayoutParams) holder.content.getLayoutParams(); params.setMargins(30, params.topMargin, params.rightMargin, params.bottomMargin); params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); holder.content.setLayoutParams(params); holder.content.setBackgroundResource(R.drawable.sent); holder.content.setText(message.sent); } else { RelativeLayout.LayoutParams params = (LayoutParams) holder.content.getLayoutParams(); params.setMargins(params.leftMargin, params.topMargin, 30, params.bottomMargin); params.addRule(RelativeLayout.ALIGN_PARENT_LEFT); holder.content.setLayoutParams(params); holder.content.setBackgroundResource(R.drawable.rcvd); holder.content.setText(message.received); }return row;}My Layout file
My Drawables - rcvd/send
View the original article here
ليست هناك تعليقات:
إرسال تعليق