Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically change RelativeLayout toRightOf to toLeftOf

i'm making chat app. I want to put timestamp besides the chat bubble. The messages will be aligned left (for received) and right (for sent).

So, I'm looking for a way to change toLeftOf to toRightOf. For now, it looks like this:

Timestamp wrongly aligned

Here's my XML code for each message (I have ArrayAdapter that handle this)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/wrapper"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/bubble"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#ffffff" />

        <TextView
            android:id="@+id/timeStamp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/bubble" />

</RelativeLayout>

I googling around and found this, but doesn't work:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
     RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if(isReceivedMessage){
    params.addRule(RelativeLayout.RIGHT_OF, R.id.bubble);
}
else{
    params.addRule(RelativeLayout.LEFT_OF, R.id.bubble);
}
timeStampTextView.setLayoutParams(params);

Any solution? Thanks

like image 561
hrsetyono Avatar asked Nov 21 '12 07:11

hrsetyono


People also ask

What is RelativeLayout and LinearLayout in Android?

LinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets.

Is Relative layout deprecated?

@filthy_wizard: RelativeLayout is not deprecated. PercentRelativeLayout is deprecated.

Which XML attribute can be used to put a view in front of other views?

Which XML attribute can be used to put a view in front of other views? bringToFront()” method will let you a perfect indicator view :) Also, there is another option to use a parent-children relationship views.

Is a ViewGroup that displays child views in relative position?

RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).


2 Answers

Try this to remove the toRightOf and toLeftOf property from any view.I am removing these properties from textview.

    TextView header2=(TextView) findViewById(R.id.text_view_header_shoppinglist);
        RelativeLayout.LayoutParams mLayoutParams2=(RelativeLayout.LayoutParams)header2.getLayoutParams();
        mLayoutParams2.addRule(RelativeLayout.LEFT_OF,0);
        mLayoutParams2.addRule(RelativeLayout.RIGHT_OF,0);
like image 166
Deepak Sharma Avatar answered Nov 11 '22 19:11

Deepak Sharma


Rewrite
Remove the attribute layout_toRightOf from the current TextView, then create two different LayoutParams: one for toRightOf and one for toLeftOf. You can switch the parameters depending on your needs.

An alternative is to use a custom adapter with two layouts. This approach should be faster since you aren't re-arranging the layout at run-time but maintaining two separate lists of "Reply" and "Send" layouts in the view recycler.

(As a note, API 17 introduced removeRule() but this won't help just yet.)

like image 24
Sam Avatar answered Nov 11 '22 17:11

Sam