Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set layout property from code

How can I set the property of an TextView which comes under a RelativeLayout from the code.

Here s my layout:

<RelativeLayout
    android:id="@+id/team_left"
    android:layout_width="wrap_content"
    android:layout_height="50dip"
    android:layout_alignParentLeft="true">
    <ImageView
        android:id="@+id/flag_left"     
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/img"/>
    <TextView
        android:id="@+id/country_left"
        android:textColor="@color/txt_color"  
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</RelativeLayout>

How can I set the properties like "android:layout_toRightOf="@id/flag_left" for the textview from the code.

like image 929
Dijo David Avatar asked Apr 06 '26 17:04

Dijo David


1 Answers

You need to use RelativeLayout.LayoutParams for the textview and then use addRule.

Example:

RelativeLayout.LayoutParams params =
    new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
                                    LayoutParams.FILL_PARENT);
params.addRule(RelativeLayout.LAYOUT_ABOVE, R.id.flag_left);
tv.setLayoutParams(params);
like image 105
pankajagarwal Avatar answered Apr 09 '26 09:04

pankajagarwal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!