Reputation: 11350
I have a listview which contains 2 Textview. one textview is shown inside a bubble background.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<!-- android:background="#FFDBE2ED"-->
<LinearLayout
android:id="@+id/wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="@+id/wrapper2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<!-- android:layout_gravity="center" -->
<TextView
android:id="@+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_marginRight="5dip"
android:layout_marginLeft="5dip"
android:background="@drawable/bubble_yellow"
android:paddingLeft="10dip"
android:text="Hello bubbles!"
android:textColor="@android:color/primary_text_light" />
<TextView
android:id="@+id/sender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0dip"
android:background="#00dcdcdc"
android:paddingLeft="10dip"
android:text="Hello bubbles!"
android:textColor="#b9dcdcdc"
android:layout_below="@+id/comment"
/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
sometimes I display textview on the right, and sometimes on left
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.listitem_discuss, parent, false);
}
wrapper = (LinearLayout) row.findViewById(R.id.wrapper);
OneMessage coment = getItem(position);
countryName = (TextView) row.findViewById(R.id.comment);
countryName.setText(coment.msgText);
countryName.setGravity(!coment.sentbyuser ? Gravity.LEFT : Gravity.RIGHT) ;
sender = (TextView) row.findViewById(R.id.sender);
sender.setGravity(Gravity.LEFT) ;
countryName.setBackgroundResource(!coment.sentbyuser ? R.drawable.bubble_yellow : R.drawable.bubble_green);
wrapper.setGravity(!coment.sentbyuser ? Gravity.LEFT : Gravity.RIGHT);
return row;
}
when I display the 2 textview on the left side, everything is fine. however, when I display the 2 textview on the right side, I expect the 2 Textview to be right aligned. But so far I could't achieve that.
Upvotes: 1
Views: 627
Reputation: 7491
TextView.setGravity()
is defining how child views are poistioned see docs. You want to actually align the textView and not its children. You are going to want to use the method setLayoutParams()
see docs. I am not sure what you are trying to do with the wrapper
LayoutView. So the following code might not be everything you'll need. But it is the recommended way to align your views with a relative layout (pragmatically).
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.listitem_discuss, parent, false);
}
wrapper = (LinearLayout) row.findViewById(R.id.wrapper);
OneMessage coment = getItem(position);
countryName = (TextView) row.findViewById(R.id.comment);
countryName.setText(coment.msgText);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if(!coment.sentbyuser)
{
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
}
else
{
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
}
countryName.setLayoutParams(params);
return row;
}
If there is a second view that needs to be aligned exactly with the countryName you can use the following:
View yourOtherView = findViewById(R.id.yourOtherView);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_BELOW, R.id.comment);
params.addRule(RelativeLayout.ALIGN_RIGHT, R.id.comment);
yourOtherView.setLayoutParams(params);
That tells it to align it below your comment view and then to right align it. You might need to play around with margins, padding and other values to get it exactly right.
Good luck!
Upvotes: 3