Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change android:layout_alignParentRight runtime by java code

I have chat list view in which sender name should be left aligned and receiver's reply should be on right side, and then vice a versa when receiver becomes sender.

Here is my xml file:

<?xml version="1.0" encoding="utf-8"?>

<TextView android:id="@+id/chatmessagename"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true"
    android:textSize="16sp" android:textStyle="bold"
    android:paddingBottom="1dp" />
<TextView android:id="@+id/chatmessagedate"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"    
    android:autoLink="none" />
<TextView android:id="@+id/chatmessagetext"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@id/chatmessagename"
    android:autoLink="all" />

Now at runtime i want to change textview's layout_alignParentRight value. Is it possible to do ?

like image 208
Zoombie Avatar asked Dec 09 '22 01:12

Zoombie


2 Answers

Try:

... 
RelativeLayout.LayoutParams layoutParams =(RelativeLayout.LayoutParams)yourView.getLayoutParams();
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_.....); //ALIGN_PARENT_RIGHT / LEFT etc.
yourView.setLayoutParams(params);
... 
like image 153
Vyacheslav Shylkin Avatar answered May 25 '23 13:05

Vyacheslav Shylkin


yes is possible you can use this things:-

            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
                android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);


rlp.addRule(RelativeLayout.ALIGN_RIGHT,
                    textviewid);

form this code you get some ref..

like image 37
Sunil_Suthar Avatar answered May 25 '23 11:05

Sunil_Suthar