How to change android: layout_alignParentRight runtime by java code

I have a chat list view in which the sender's name should be left-aligned and the recipient's response should be on the right, and then vice versa when the recipient becomes the 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 the value of textview layout_alignParentRight. Can this be done?

+4
source share
3 answers

Try:

 ... RelativeLayout.LayoutParams layoutParams =(RelativeLayout.LayoutParams)yourView.getLayoutParams(); layoutParams.addRule(RelativeLayout.ALIGN_PARENT_.....); //ALIGN_PARENT_RIGHT / LEFT etc. yourView.setLayoutParams(params); ... 
+12
source

yes you can use this: -

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

generate this code, you will get some links.

+1
source

use this code

 RelativeLayout.LayoutParams _params = new RelativeLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); _params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); apps_layout.setLayoutParams(_params); 
0
source

Source: https://habr.com/ru/post/1403855/


All Articles