Reputation: 11419
I have a listview to show messages in a chat. I'd like to show the received messages on the left and the sent messages on the right. Please consider that it is not enough to align the contained views because I need to show a background of the entry and the background should be aligned too. SO the whole entry should be aligned.
Is there a way to do it?
I tried to use android:layout_gravity="right"
but it did not work.
Upvotes: 0
Views: 851
Reputation: 11419
I managed to do it by using:
LayoutParams params = (LayoutParams) frame.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, alignRight?0:1);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, alignRight?1:0);
frame.setLayoutParams(params);
what it is important is that I had to add a frame within a RelativeLayout for my entry, because params.addRule is available only for RelativeLayout. If you try to do the same with the view passed to bindView you get a different LayoutParam that does not support addRule
Upvotes: 1
Reputation: 16393
You could try setting the parameter programatically in your adapter using LayoutParams
.
Something like the following for sent messages:
LinearLayout ll = (LinearLayout) findViewById(R.id.MyLinearLayout);
// need to cast to LinearLayout.LayoutParams to access the gravity field
LayoutParams params = (LayoutParams)ll.getLayoutParams();
params.gravity = Gravity.RIGHT;
ll.setLayoutParams(params);
and the reverse (change RIGHT to LEFT) for received messages.
Upvotes: 0
Reputation: 6159
Of course! you need an Adapter. In the adapter, when you inflate the ListItem, just inflate different Items for sent and received messages!
Upvotes: 0